Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/babel/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<cwd>/')
.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] <cwd>/foo.js: Unexpected token (1:13)

> 1 | export const = ;
| ^"
`)
})

async function buildWithVite(
filename: string,
code: string,
Expand Down
21 changes: 16 additions & 5 deletions packages/babel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,22 @@ async function babelPlugin(rawOptions: PluginOptions): Promise<Plugin> {
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,
Expand Down