Skip to content

Commit

Permalink
fix: avoid loop at call TransformPluginContext#error (#2332)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description
close #2326
<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
underfin authored Sep 26, 2024
1 parent a03a0d7 commit 7c699da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/rolldown/src/plugin/transform-plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
import type { LoggingFunctionWithPosition, RollupError } from '../rollup'
import { normalizeLog } from '../log/logHandler'
import { PluginContext } from './plugin-context'
import { augmentCodeLocation } from '../log/logs'
import { augmentCodeLocation, error, logPluginError } from '../log/logs'
import { PluginContextData } from './plugin-context-data'
import { NormalizedInputOptions } from '..'
import type { Plugin } from './index'
Expand Down Expand Up @@ -41,14 +41,14 @@ export class TransformPluginContext extends PluginContext {
this.warn = getLogHandler(this.warn)
this.info = getLogHandler(this.info)
this.error = (
error: RollupError | string,
e: RollupError | string,
pos?: number | { column: number; line: number },
): never => {
if (typeof error === 'string') error = { message: error }
if (pos) augmentCodeLocation(error, pos, moduleSource, moduleId)
error.id = moduleId
error.hook = 'transform'
return this.error(error)
if (typeof e === 'string') e = { message: e }
if (pos) augmentCodeLocation(e, pos, moduleSource, moduleId)
e.id = moduleId
e.hook = 'transform'
return error(logPluginError(normalizeLog(e), plugin.name || 'unknown'))
}
// this.getCombinedSourcemap = () => JSON.parse(inner.getCombinedSourcemap())
}
Expand Down
21 changes: 21 additions & 0 deletions packages/rolldown/tests/plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,24 @@ describe('Plugin closeBundle hook', async () => {
}
})
})

test('call transformContext error', async () => {
try {
const build = await rolldown({
input: './main.js',
cwd: import.meta.dirname,
plugins: [
{
transform() {
this.error('transform hook error')
},
},
],
})
await build.write({})
} catch (error: any) {
expect(error.message).toMatchInlineSnapshot(
`"Rolldown internal error: GenericFailure, RollupError: transform hook error"`,
)
}
})

0 comments on commit 7c699da

Please sign in to comment.