-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Unexpected content if there's import()
in a js file and import with ?url
#11266
Comments
P.S. import WorkerURL from "@/vendors/worker.js?url"
// WorkerURL = "@fs/src/vendors/worker.js" instead of import WorkerURL from "@/vendors/worker.js?url"
// WorkerURL = "/src/vendors/worker.js" and things work but I am unable to reproduce it. |
Related: #5690 |
Enable module for worker. const worker = new Worker(WorkerJSURL, {
+ type: "module"
}); MDN: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker#syntax |
The worker creation code is emscripten generated and I cannot modify it. Otherwise i could use the following code to create worker. import Worker from './woker.js?worker'
const worker = new Worker() From doc, user will expect the Also the behavior is different between |
A possible fix way: |
I am sorry for misunderstanding. I guest you gonna to keep workers splited out of main chunk. Just like this PR prevents assets bundling inline? |
I do want to keep workers split out of main chunk. But I don’t think this is related to that PR. P.S that PR is related to build stage. However this issue only happens in P.P.S In other word, vite cannot distinguish a js file is imported by
Or
|
I think this is an unintended bug. There's a lot of edge cases and this would have been easy to miss. The import analysis plugin needs additional heuristics for when its in a classic worker and to use |
Somewhat related to #12995, maybe we can inline the |
@bluwy that looks amazing! I was halfway through writing a similar solution, I should've checked first 😅 . I think inlining the function is the way to go for sure 👍 |
I don’t think inline the |
Also when a user use |
@rwv in Vite, if you import the file regardless of |
My workaround is to put worker file in public folder. However |
We can't change the behaviour of |
I'm tearing my hair out trying to get this to work. Same scenario. I've already put the worker js file in the assets folder but still vite insists on adding that annoying import. why?? |
Thank you for your awesome PR! 🎉 |
Describe the bug
I import a
worker.js
's URL usingAnd vite serves worker URL to browser with added file header if there's a
import()
in the worker fileand cause error
Uncaught SyntaxError: Cannot use import statement outside a module
Reproduction
https://github.com/rwv/vite-import-url-issue
Steps to reproduce
Run
npm run dev
andnpm run build
and open the web page.System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: