-
-
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
When using import
inside classic worker, syntax error happens during dev but build success
#8470
Comments
@sapphi-red I think we just need to support (case 1) import 'a.js' instead of (case 2) import('a.js') IIUC, https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts |
oh, but the |
Just in case, Yep, |
I test |
I don't think it will work and it is actually not working in my reproduction. My understanding is:
So it does not work only if it is imported with |
worker.js import('./modules/module2.js').then(module0 => {
self.postMessage({
type: 'classic-worker-import',
content: module0.default
})
}) ./modules/module2.js export * from './module0'
export * from './module1'
export const msg2 = 'module2' It seems |
dynamic import are supported in both classic and module worker.
It is classic if
Files imported by static or dynamic import is treated as ESM (even if they are import from a classic worker/script). |
so the issue asks for
|
To be honest, I didn't know that dynamic import works inside classic worker/script. At the time I created this issue, I was only thinking about static imports. Also, now I think supporting static import implicitly in classic worker is confusing as it misaligns with the spec. new Worker(new URL('./foo.js', import.meta.url), { type: 'classic', as: 'module' }) Maybe we could just stop static import working in classic worker with build? |
If support static import working in classic worker, I think we can transform code like this Promise.all([
import('./a.js'),
import('./b.js'),
]).then(mods => {
const [
moda,
modb
] = mods
.......
}) I think this is acceptable. but dynamic import support in classic worker it seems can't mock. because we can't make dynamic import deps to load on demand, we just can make them to static import and handle it. if(flag) {
import('./foo.js').then((mods) => {
self.postMessage(mods)
})
} tobe import __vite_worker_dynamic_import_0 from './foo.js'
if(flag) {
Promise.resolve({default: __vite_worker_dynamic_import_0}).then((mods) => {
self.postMessage(mods)
})
} and transform by rollup BTW, To stop static import working in classic worker with the build is okay too 😂 |
My first thought was this one. But the notation/syntax needs to be considered. |
I think the better way is to compile it but it will use more performance. I think now better to alert document |
Following this issue. This is the biggest inhibitor to running workers with imports in dev mode with Firefox. I can't be the only one who develops against Firefox, right? |
currently stuck because of vitejs/vite#8470
Some colleagues prefer Firefox for development and we would like to debug on FF as well. This issue prevents development on Firefox for any project that uses Workers with imports. Is there a way to tell vite to inline imports into the worker file during development? |
Looks like Firefox 114 will support
EDIT: Confirmed.
|
Related: #10697 (feature request to always bundle workers) |
Hello! I just ran into this issue and spent several hours trying to understand why the service worker of my application was throwing an error. Now that Firefox apparently supports ECMAScript modules for the Worker() constructor, what would it take for Vite PWA to use the module type and enable the development of service workers on Firefox? |
Firefox support for module import in service workers is still being implemented. It's tracked here: |
Thank you for the link! |
I was running into this as well so I made a plugin to implement the functionality: https://github.com/itsjohncs/vite-plugin-prebundle-workers. But I also think this would make sense to do by default in Vite. |
I tried to use and I still get the "Cannot use import statement outside a module" error when the worker tries to import some modules |
@dariusve open up an issue in itsjohncs/vite-plugin-prebundle-workers with some more info (configuration and code that's failing) and I'll try and help you debug. My guess is the plugin isn't configured to bundle the right file(s), but there may be something else going on. |
Describe the bug
When
import
is used in classic worker, the following error happens during dev.But build works with it and the bundle seems to be correctly bundled.
According to the docs,
import
can be used inside classic worker if worker is imported with query suffixes.There is no mention about constructor type worker import. But if it is the recommended way, I think it would be good to support it too. Also it is useful for #8466.
#8466 (comment) might be related.
Reproduction
https://stackblitz.com/edit/vitejs-vite-mhmcjy?file=main.js
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: