diff --git a/src/rspack/loaders/transform.ts b/src/rspack/loaders/transform.ts index 205a9cb5..2b53e875 100644 --- a/src/rspack/loaders/transform.ts +++ b/src/rspack/loaders/transform.ts @@ -14,19 +14,30 @@ export default async function transform( const id = this.resource const context = createContext(this) - const res = await plugin.transform.call( - Object.assign( - {}, - this._compilation && createBuildContext(this._compiler, this._compilation, this), - context, - ), - source, - id, - ) - if (res == null) - callback(null, source, map) - else if (typeof res !== 'string') - callback(null, res.code, map == null ? map : (res.map || map)) - else callback(null, res, map) + try { + const res = await plugin.transform.call( + Object.assign( + {}, + this._compilation && createBuildContext(this._compiler, this._compilation, this), + context, + ), + source, + id, + ) + + if (res == null) + callback(null, source, map) + else if (typeof res !== 'string') + callback(null, res.code, map == null ? map : (res.map || map)) + else callback(null, res, map) + } + catch (error) { + if (error instanceof Error) { + callback(error) + } + else { + callback(new Error(String(error))) + } + } } diff --git a/src/webpack/loaders/transform.ts b/src/webpack/loaders/transform.ts index 44bdfa79..9325f83a 100644 --- a/src/webpack/loaders/transform.ts +++ b/src/webpack/loaders/transform.ts @@ -10,23 +10,34 @@ export default async function transform(this: LoaderContext, source: string return callback(null, source, map) const context = createContext(this) - const res = await plugin.transform.call( - Object.assign({}, createBuildContext({ - addWatchFile: (file) => { - this.addDependency(file) - }, - getWatchFiles: () => { - return this.getDependencies() - }, - }, this._compiler!, this._compilation, this), context), - source, - this.resource, - ) - if (res == null) - callback(null, source, map) - else if (typeof res !== 'string') - callback(null, res.code, map == null ? map : (res.map || map)) - else - callback(null, res, map) + try { + const res = await plugin.transform.call( + Object.assign({}, createBuildContext({ + addWatchFile: (file) => { + this.addDependency(file) + }, + getWatchFiles: () => { + return this.getDependencies() + }, + }, this._compiler!, this._compilation, this), context), + source, + this.resource, + ) + + if (res == null) + callback(null, source, map) + else if (typeof res !== 'string') + callback(null, res.code, map == null ? map : (res.map || map)) + else + callback(null, res, map) + } + catch (error) { + if (error instanceof Error) { + callback(error) + } + else { + callback(new Error(String(error))) + } + } }