-
-
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
import statement in Workers #4586
Comments
To use workers in development you have to use a suported browser (see https://caniuse.com/mdn-api_worker_worker_ecmascript_modules). In production the worker is bundled in a new conext with no import statements. Just as a refrence here the bug in the Firefox bugtracker for module Worker https://bugzilla.mozilla.org/show_bug.cgi?id=1247687. |
@mathe42 Yes, it must inline es modules in bundle. Is it possible now with rollupjs setting? |
In production (and current vite version) worker work in firefox. |
Yes. But we cant use es modules inside worker in FF now. I dont find a way how inline es modules inside worker for npm run build |
It is done automaticly no options required. Just build it and then open result in FF. |
I dont see this behavior.Please check MVP. npm run dev and you will see in console "hello world" from wasm. It is working only in dev mode. |
Oh I see the problem. Should be easy to fix... |
Created a PR (#4741) is a simple mistake 😂. |
@mathe42 thanks, You should understand - it is a unbelievable problem if you are just install vite yesterday :-) |
I had another look at your code you don't import a Worker! This is why it dosn't work! Use import MyWorker from './worker-file?worker'
const worker = new MyWorker() The problem in my PR doesn't fix your problem. Use the above and in production it works in FF. |
Thank you! Yes it working. But
Can it be solved if we include deps via https://vitejs.dev/config/#dep-optimization-options ? |
Vite doesn't parse your file where ever posible. So there is no way to detect a constructor call (or wasm calls). You can write your own vite plugin that can does that (not that complicated) Quick idea for Worker: in tranform search code for Should be a plugin with 20 Lines of code or so. But it has edge cases where it will transform to much. And there is no way to do that properly on the fly. Tip for emscripten glue-code: Put the wasm file in the public folder this way vite don't need to handle that file. You might want to check if the glue code expects some subfolders. For workers I think comlink is so nice that I want to have the custom imports for type checking. (I use allways Typescript.) |
@mathe42 thank you. Can I automate coping wasm file to asset folder? Vite does not discover wasm files. Or better to do it in plugin? |
This thing makes it harder to test in Firefox, it would be nice to be able to turn it off: |
Describe the bug
Due Firefox limit to support import statement in a web workers, how we can create production bundle?
I get "SyntaxError: import declarations may only appear at top level of a module" in FF.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
Reproduction
use
import {myExport} from '/modules/my-module.js';
inside worker.System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: