Skip to content
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 import javascript files and local dependencies from web workers #12853

Closed
7 tasks done
m4rch3n1ng opened this issue Apr 13, 2023 · 2 comments
Closed
7 tasks done
Labels
duplicate This issue or pull request already exists

Comments

@m4rch3n1ng
Copy link

m4rch3n1ng commented Apr 13, 2023

Describe the bug

i have created a default, vite template svelte (with typescript) and i am trying to import a Worker (from src/lib/worker.js, that imports code from both a local file (src/lib/test.ts) and a local dependency ("comlink"). in dev mode it works fine but after building and running it using a static file server (for example sirv-cli) it silently fails (i.e. just doesn't execute). if i remove the imports to local files (like i have in the fix branch) it works again.

note: since this i have only tested this with svelte configuration (both regular vite svelte and svelte-kit) the issue might come from the svelte plugin for vite (@sveltejs/vite-plugin-svelte), but since the transpiling is done by vite i suspect the issue comes from here

Reproduction

https://github.com/m4rch3n1ng/vite-webworker-bug-reproduction

Steps to reproduce

info

if you see two console logs, one labeled as "App.svelte" and one labeled as "worker.js", then it's working.
if you only see the "App.svelte" console log then the web worker is not working

broken

$ git switch main
$ npm install
$ npm run build
$ npx sirv-cli dist
works in dev mode
$ npm run dev

fix (annoying and non-optimal)

$ git switch fix
$ npm install
$ npm run buid
$ npx sirv-cli dist

System Info

System:
    OS: Linux 6.2 Arch Linux
    CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
    Memory: 8.33 GB / 15.56 GB
    Container: Yes
    Shell: 3.6.1 - /usr/bin/fish
  Binaries:
    Node: 19.8.1 - /usr/bin/node
    Yarn: 1.22.19 - ~/.local/bin/yarn
    npm: 9.6.3 - ~/.local/bin/npm
  Browsers:
    Chromium: 112.0.5615.49
  npmPackages:
    vite: ^4.2.0 => 4.2.1

Used Package Manager

npm

Logs

No response

Validations

m4rch3n1ng added a commit to m4rch3n1ng/blictionary that referenced this issue Apr 14, 2023
@sapphi-red
Copy link
Member

Duplicate of #6757

Currently Vite doesn't bundle files imported with ?url and simply treats as a simple asset.
A workaround is to use: new Worker(new URL('./worker.js', import.meta.url), { type: "module" }) (docs).

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Apr 14, 2023
@m4rch3n1ng
Copy link
Author

thank you so much, i do think it should be explicitly stated in the docs that imports like that don't actually get bundled.
i didn't think to use the new URL method, since that threw a build error on my production build (and i forgot to test in the reproduction) but that seems to be the fault of sveltejs/kit#9528.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants