-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Workers are not imported correctly despite the worker syntax seemingly being correct, works in parcel #11672
Comments
This is similar to #8427. A workaround is to set |
Thanks @sapphi-red – so are the docs at https://vitejs.dev/guide/features.html#import-with-constructors partially incorrect? I think that's exactly the same syntax. Seems something like "this works in your local project but not in a dependency" and "this will not work anymore once you release your package to others" would be important to note there, including how to work around. Additionally, does that mean this is by design? It's unclear to me if / why it's not considered a bug in vite. |
Yeah, we should probably have a document that explains dep-optimizer doesn't support every feature on the features page. IIUC this is just not implemented. |
The
import { append } from "./util"
self.onmessage = (msg) => {
self.postMessage(append(msg.data))
}
export function append(s) {
return s + ' world'
} Somewhere in main/entry point: const worker = new Worker(new URL('./worker.ts', import.meta.url))
worker.postMessage('hello')
worker.onmessage = (evt) => {
console.log(evt.data)
} And just to be sure, import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: {
exclude: ['worker']
}
}); The browser console reports the following error:
If you manually inline the |
Adding
Maybe a package should be able to declare to vite that it should be excluded from |
Even better, vite analyzes dependencies for |
With the raise of first-offline database solutions like https://github.com/rhashimoto/wa-sqlite, https://github.com/powersync-ja/powersync-js/blob/main/README.md, https://github.com/sqlite/sqlite-wasm, this is a more pressing issue. |
Workaround is not working for me. I am trying to use - sql.js-httpvfs in a svelte project. My vite.config.ts looks like this. export default defineConfig({
plugins: [sveltekit(), purgeCss({
safelist: {
// any selectors that begin with "hljs-" will not be purged
greedy: [/^hljs-/],
},
}),],
optimizeDeps: {
exclude: ["sql.js-httpvfs"],
}
}); I get the following error
I am kinda new to this, any help is appreciated. |
Yeah I'm having a horrible time trying to use a worker importing Three.js stuff using import statements and it causing the main thread to think that document is not defined? Not sure what's going on... Edit: calling Edit edit: ah I don't want to use a SharedWorker so that's not ideal D: Edit edit edit: I was checking |
Also plucking my hair out while trying to make https://github.com/mmomtchev/sqlite-wasm-http work. |
@DanielHabenicht I ended up having more issues with this and solved it by splitting the repo into subpath exports and exporting "worker utils" rather than the full worker constructors and letting my consuming app code create the worker using the utils (this latter part might not be as necessary, the first part with the sub-path'ing seemed to help stop Vite from bundling up unnecessary code into the bundle from spaghetti'ish, uncareful imports in the codebase) |
Well, I can not influence what is exported from |
Hi. We have some workers with
It is not ideal but it works (as it's described here #15547). One of our customer have integrated our library into application on server with strict CSP. When he wanna use it, he need to allow |
Hi. I have the same problem with using Vite+React and library memgraph orb. It generates error:
Adding library to optimizeDeps.exclude didn't help. |
Describe the bug
On an existing project that uses
parcel
, I switched tovite
but that results in web workers not working anymore / returning undefined.Reference issue with the dev verifying that the worker syntax matches vite's expectations:
vite
instead ofparcel
breaks web workers gkjohnson/three-gpu-pathtracer#355Vite worker docs, which use the same syntax as the project at hand:
https://vitejs.dev/guide/features.html#import-with-constructors
My best guess is that the bug here is that a worker is used from a sub-sub package (three-gpu-pathtracer uses the worker from three-mesh-bvh) and that somehow doesn't work in vite right now?
Links to the source code to easily follow the chain until the actual worker:
(identical to the index.js in the repro project)
(the actual worker)
Reproduction
https://stackblitz.com/edit/vitejs-vite-k1ccfw?file=index.js
Steps to reproduce
Steps to reproduce in the StackBlitz project linked above:
npm run dev
Uncaught (in promise) Error: GenerateMeshBVHWorker: undefined at worker.onerror
Compare behaviour to https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/index.html
Steps to reproduce locally:
npm install --save-dev vite
"start-vite": "cd example && vite --host",
in scriptsnpm run start-vite
Uncaught (in promise) Error: GenerateMeshBVHWorker: undefined
System Info
Reproduces on any system I tried on.
On StackBlitz:
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 16.14.2 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 7.17.0 - /usr/local/bin/npm npmPackages: vite: ^4.0.4 => 4.0.4
Used Package Manager
npm
Logs
Uncaught (in promise) Error: GenerateMeshBVHWorker: undefined at worker.onerror
Validations
The text was updated successfully, but these errors were encountered: