From d633b2dd1b8157d52c7e74089ebcb3a6959e2859 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 26 Feb 2026 23:09:44 +0900 Subject: [PATCH 1/2] feat(babel): format babel errors --- packages/babel/src/index.test.ts | 21 +++++++++++++++++++++ packages/babel/src/index.ts | 21 ++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/babel/src/index.test.ts b/packages/babel/src/index.test.ts index 63b3cf9..4887cf3 100644 --- a/packages/babel/src/index.test.ts +++ b/packages/babel/src/index.test.ts @@ -549,6 +549,27 @@ describe('per-environment state isolation', () => { }) }) +test('babel syntax error produces enhanced error message', async () => { + const err = await build('foo.js', 'export const = ;', { + plugins: [identifierReplaceBabelPlugin('foo', true)], + }).catch((e) => e) + const normalized = err.message + .replace(/\r\n/g, '\n') + .replace(/[A-Z]:[\\/][^\s:]+[\\/](?=foo\.js)/gi, '/') + .replace(/\n {4,}at .+/g, '') + .replace(/\nCaused by:\n[\s\S]*$/, '') + .trim() + expect(normalized).toMatchInlineSnapshot(` + "Build failed with 1 error: + + [plugin @rolldown/plugin-babel] foo.js:1:13 + RolldownError: [BabelError] /foo.js: Unexpected token (1:13) + + > 1 | export const = ; + | ^" + `) +}) + async function buildWithVite( filename: string, code: string, diff --git a/packages/babel/src/index.ts b/packages/babel/src/index.ts index 7767961..e7f4707 100644 --- a/packages/babel/src/index.ts +++ b/packages/babel/src/index.ts @@ -82,11 +82,22 @@ async function babelPlugin(rawOptions: PluginOptions): Promise { return } - const result = await babel.transformAsync( - code, - // oxlint-disable-next-line typescript/no-unsafe-type-assertion - loadedOptions as unknown as babel.InputOptions, - ) + let result: babel.FileResult | null + try { + result = await babel.transformAsync( + code, + // oxlint-disable-next-line typescript/no-unsafe-type-assertion + loadedOptions as unknown as babel.InputOptions, + ) + } catch (err: any) { + this.error({ + message: `[BabelError] ${err.message}`, + loc: err.loc, + pos: err.pos, + cause: err, + pluginCode: `${err.code}:${err.reasonCode}`, + }) + } if (result) { return { code: result.code ?? undefined, From 6897c46633be21a43cc738f0d517148270bfdfef Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 26 Feb 2026 23:11:11 +0900 Subject: [PATCH 2/2] chore: fix --- packages/babel/src/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel/src/index.test.ts b/packages/babel/src/index.test.ts index 4887cf3..37143b1 100644 --- a/packages/babel/src/index.test.ts +++ b/packages/babel/src/index.test.ts @@ -555,7 +555,7 @@ test('babel syntax error produces enhanced error message', async () => { }).catch((e) => e) const normalized = err.message .replace(/\r\n/g, '\n') - .replace(/[A-Z]:[\\/][^\s:]+[\\/](?=foo\.js)/gi, '/') + .replace(/(?:[A-Z]:)?[\\/][^\s:]+[\\/](?=foo\.js)/gi, '/') .replace(/\n {4,}at .+/g, '') .replace(/\nCaused by:\n[\s\S]*$/, '') .trim()