unjs / unplugin

Unified plugin system for Vite, Rollup, Webpack, esbuild, Rolldown, and more

Home Page:https://unplugin.unjs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

buildStart hook is not async in webpack

brawaru opened this issue · comments

Environment

unplugin 1.3.1
webpack 5.76.2
Node.js v16.14.2

Reproduction

  1. Have buildStart perform async operation.
  2. Have transform hook rely on ordered completion buildStart and 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 buildStart is async and can be used to perform initial initialisation for transform hook invocations.
  • transform hook seems to be correctly async, which means you can await for buildStart fulfillment in it, which involves moving async logic of buildStart to 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.

It does not seem to be asynchronous, for webpack

I tried to solve it without success