Skip to content
Open
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
44 changes: 27 additions & 17 deletions src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@ export default async function load(this: LoaderContext<any>, source: string, map

const context = createContext(this)
const { handler } = normalizeObjectHook('load', plugin.load)
const res = await handler.call(
Object.assign({}, createBuildContext({
addWatchFile: (file) => {
this.addDependency(file)
},
getWatchFiles: () => {
return this.getDependencies()
},
}, this._compiler!, this._compilation, this), context),
normalizeAbsolutePath(id),
)
try {
const res = await handler.call(
Object.assign({}, createBuildContext({
addWatchFile: (file) => {
this.addDependency(file)
},
getWatchFiles: () => {
return this.getDependencies()
},
}, this._compiler!, this._compilation, this), context),
normalizeAbsolutePath(id),
)

if (res == null)
callback(null, source, map)
else if (typeof res !== 'string')
callback(null, res.code, res.map ?? map)
else
callback(null, res, map)
if (res == null)
callback(null, source, map)
else if (typeof res !== 'string')
callback(null, res.code, res.map ?? map)
else
callback(null, res, map)
}
catch (error) {
if (error instanceof Error) {
callback(error)
}
else {
callback(new Error(String(error)))
}
}
}
13 changes: 13 additions & 0 deletions test/unit-tests/webpack/loaders/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ describe('load function', () => {

expect(mockCallback).toHaveBeenCalledWith(null, transformedCode, map)
})

it('should call callback with the error if the handler throws', async () => {
const source = 'source code'
const map = 'source map'
const error = new Error('Handler error')
const pluginLoadHandler = vi.fn().mockRejectedValue(error)
mockLoaderContext.query.plugin.load = pluginLoadHandler

await load.call(mockLoaderContext as any, source, map)

expect(pluginLoadHandler).toHaveBeenCalled()
expect(mockCallback).toHaveBeenCalledWith(error)
})
})