-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
npx vite build
breaks module worker instantiation
#14499
Comments
Your project has a following circular dependency and that is causing #7015.
|
Duplicate of #7015 |
Hm, this is code produced by a straightforward const from = /* lazy Promise implementation */
var dynamic4x4x4Solver = from(() => import("./search-dynamic-solve-4x4x4-JQRNWKAG.js")); My understanding is that this is well-formed code, since there is no import loop at runtime. Since Are you able to recommend any workarounds so that folks can use my library with Vite? |
Assuming that you don't need the nested workers feature, this configuration seems to work. import { defineConfig } from 'vite'
// https://github.com/vitejs/vite/blob/ec7ee22cf15bed05a6c55693ecbac27cfd615118/packages/vite/src/node/plugins/workerImportMetaUrl.ts#L127-L128
const workerImportMetaUrlRE =
/\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/g
export default defineConfig({
worker: {
format: 'es',
plugins: [
{
name: 'foo',
enforce: 'pre',
transform(code, id) {
if (code.includes('new Worker') && code.includes('new URL') && code.includes('import.meta.url')) {
const result = code.replace(workerImportMetaUrlRE, `((() => { throw new Error('Nested workers are disabled') })()`)
return result
}
}
}
],
rollupOptions: {
output: {
chunkFileNames: 'assets/worker/[name]-[hash].js',
assetFileNames: 'assets/worker/[name]-[hash].js'
}
}
}
}) https://stackblitz.com/edit/vitejs-vite-sdcywk?file=vite.config.js |
Thanks, that's pretty intense, but seems to do the trick! 🤩 Are there any changes to Vite or my code that could easily make so that this works out of the box? It's unlikely that I'll be able to avoid dynamic cyclic dependency in the published build in the near future, due to other intricate bundler compat workarounds for worker instantiation like evanw/esbuild#312 (comment) |
Unfortunately, I think there aren't other than fixing #7015. |
Describe the bug
I publish an ESM-only library that uses web workers.
npx vite serve
handles it perfectly, butnpx vite build
fails on the following lines:https://github.com/cubing/cubing.js/blob/4afc6d727549aadc1e82abf52d531565953e7a9a/src/cubing/search/worker-workarounds/index.ts#L29-L31
(This difference between
dev
andbuild
may be because workers are not built in dev mode?)From the stack trace, it looks like the failure is because the CommonJS resolver is unexpectedly run even when:
es2022
."type": "module"
(which is also the recommended way).Reproduction
https://stackblitz.com/edit/vitejs-vite-woswba?file=main.js
Steps to reproduce
Run the following:
Expected: The code builds.
Observed:
System Info
Used Package Manager
npm
Logs
Click to expand!
Validations
The text was updated successfully, but these errors were encountered: