-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Cannot execute Classic Worker in dev mode due to imports injected by Vite #7019
Comments
workerOptions if make all worker to |
if use user config and code is dynamic. can't match all. PS: |
I think the compatibility mode should be a global configuration. Maybe we can provide a configuration that all workers are |
I think it would be fair to assume that |
It may be not perfect, but at least it should work. |
I think we can detect the case where |
Describe the bug
Problem
When I created and executed a classic Web Worker in dev mode, browsers will throw an error:
Here is how I created the classic Web Worker:
And, here is the Web Worker file. Note that it doesn't contain any import statements or dynamic imports:
Possible Reason
In dev mode, Vite will inject a special module
/@vite/env
for shimming environment variables, and it will introduce an import statement. The internal "worker" and "workerImportMetaUrl" plugin inject:vite/packages/vite/src/node/plugins/worker.ts
Line 63 in 33f9671
vite/packages/vite/src/node/plugins/workerImportMetaUrl.ts
Line 28 in 33f9671
Then, the worker script will become: (for the example I mentioned above)
Because import statements aren't allowed in classic Web Worker, browsers threw that error.
Possible Solution
I tried using
importScripts
which is available in Web Worker to import that/@vite/env
module. It works for my case, since it's a Classic Worker, not a Module Worker.However, when running Vite's tests, they failed. It reported:
so I don't have ideas about fixing this problem.
Q & A
A: No, because in my real case, that snippet about creating worker (
new Worker(/* */)
) comes from a third-party library, which I'm not able to control. Also, I use Firefox, fixing this bug would be better.Reproduction
https://stackblitz.com/edit/vitejs-vite-y9bsxv?file=main.js&terminal=dev
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: