Repository navigation
Open
Labels
Description
Environment
unplugin 1.3.1
webpack 5.76.2
Node.js v16.14.2
Reproduction
- Have
buildStartperform async operation. - Have
transformhook rely on ordered completionbuildStartand its result.
StackBlitz: https://stackblitz.com/edit/webpack-webpack-js-org-1qvdhq?file=package.json&view=editor (pnpm start to run webpack programmatically, pnpm test to run tests which will also do the build programatically).
Describe the bug
buildStart is not an async hook, and invoking await will immediately continue the build pipeline, which will have unexpected consequences.
Vitest:
-- webpack call
-- webpack end
-- buildStart call ← buildStart waits 1 second to resolve a promise
-- transform call ← transform called before buildStart is fulfilled
× transform thrown Error: transform method was called either before buildStart call was completed or after it has thrown an error
-- transform end
√ buildStart return ← timer fires, buildStart fulfills
-- buildStart end
(Test timeout)
Node.js:
-- webpack call
-- webpack end
-- buildStart call
-- transform call
transform call without without buildStart completion, throwing!
× transform thrown Error: transform method was called either before buildStart call was completed or after it has thrown an error
-- transform end
UNHANDLED EXPECTION
Error: transform method was called either before buildStart call was completed or after it has thrown an error
(Process halted with error code)
Additional context
- In Rollup/Vite realm
buildStartis async and can be used to perform initial initialisation fortransformhook invocations. transformhook seems to be correctly async, which means you can await forbuildStartfulfillment in it, which involves moving async logic ofbuildStartto a separate function and then also creating another plugin variable in which you will store promise returned by that separate async function call. But it's not obvious and still feels like a mistake/oversight.
Logs
N/A.Reacted by Loris Sigrist, northernCold, Valik Ulyanov and Tomina
Activity
wChenonly commentedon Jun 12, 2023
It does not seem to be asynchronous, for webpack
I tried to solve it without success
LorisSigrist commentedon Nov 27, 2023
The workaround right now is to add webpack specific code to await the promise.
wChenonly commentedon Dec 1, 2023
yes,At present, this can only be done first.
fix: buildStart hook is not async in webpack
SoonIter commentedon Dec 4, 2024
some investigations
the
makehook is aAsyncParallelhook in webpack, soloader transformandbuildStartis parallel inmakestageunplugin/src/webpack/index.ts
Lines 183 to 203 in 1b697de
this issue will not appear in Rspack, because currently Rspack's make hook is
AsyncSeriesHookhttps://github.com/web-infra-dev/rspack/blob/4bc1d680fa37c5dd7a40c7a60febd832ea3a03f9/crates/rspack_core/src/compiler/mod.rs#L33
Unfortunately, there is no
AsyncSeriesHookwith compilation before makethisArg of buildStart is
contextOptionsFromCompilation, so there are two ways to solve this issue:contextbut not supportasync(now)asyncbut not supportcontext: to runbuildStartinbeforeCompilehook without compilation, sothisArgcannot be passed tobuildStartIMO, to comment that buildStart in webpack only supports sync is a good solution, and let users who wants to use async use workaround #293 (comment)