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

Workers are not imported correctly despite the worker syntax seemingly being correct, works in parcel #11672

Open
7 tasks done
hybridherbst opened this issue Jan 12, 2023 · 9 comments
Labels

Comments

@hybridherbst
Copy link

hybridherbst commented Jan 12, 2023

Describe the bug

On an existing project that uses parcel, I switched to vite 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 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:

Reproduction

https://stackblitz.com/edit/vitejs-vite-k1ccfw?file=index.js

Steps to reproduce

Steps to reproduce in the StackBlitz project linked above:

  • Run npm run dev
  • Note page loads
  • Note page stops loading and console logs error 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:

  • fork https://github.com/gkjohnson/three-gpu-pathtracer
  • run npm install --save-dev vite
  • in package.json, add "start-vite": "cd example && vite --host", in scripts
  • run npm run start-vite
  • visit the page
  • note loading works fine, then the MeshBVH worker returns undefined and logs to the console: 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

@sapphi-red
Copy link
Member

This is similar to #8427.
new Worker(new URL('./foo.js', import.meta.url)) is not supported by the dep optimizer.

A workaround is to set optimizeDeps.exclude: ['three-gpu-pathtracer'].

@sapphi-red sapphi-red added has workaround feat: deps optimizer Esbuild Dependencies Optimization labels Jan 20, 2023
@hybridherbst
Copy link
Author

hybridherbst commented Jan 24, 2023

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.

@sapphi-red
Copy link
Member

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.

@akazukin5151
Copy link

akazukin5151 commented Feb 3, 2023

The optimizeDeps.exclude workaround doesn't work if your worker file has imports. For example:

worker.ts:

import { append } from "./util"

self.onmessage = (msg) => {
  self.postMessage(append(msg.data))
}

util.ts:

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, vite.config.ts is:

import { defineConfig } from 'vite';

export default defineConfig({
  optimizeDeps: {
    exclude: ['worker']
  }
});

The browser console reports the following error:

SyntaxError: import declarations may only appear at top level of a module

If you manually inline the append function into worker.ts and remove the import, it works.

@thewtex
Copy link

thewtex commented Oct 17, 2023

SyntaxError: import declarations may only appear at top level of a module

const worker = new Worker(new URL('./worker.ts', import.meta.url))

Adding { type: 'module' } may be needed?


This is similar to #8427. new Worker(new URL('./foo.js', import.meta.url)) is not supported by the dep optimizer.

A workaround is to set optimizeDeps.exclude: ['three-gpu-pathtracer'].

Maybe a package should be able to declare to vite that it should be excluded from optimizeDeps in its package.json? So everything that depends on a given package, three-gpu-pathtracer in this case, does not need to make an entry in optimizeDeps.exclude?

@thewtex
Copy link

thewtex commented Oct 18, 2023

Maybe a package should be able to declare to vite that it should be excluded from optimizeDeps in its package.json?

Even better, vite analyzes dependencies for new Worker(new URL('./*.worker.js', import.meta.url)), and does not try to optimized the dependency. If / when the functionality is available, the optimization can be re-enabled.

@guillempuche
Copy link

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.

@GibreelAbdullah
Copy link

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

ReferenceError: Worker is not defined
    at t.createDbWorker (/home/alfi/Projects/Hadith-Hub/node_modules/sql.js-httpvfs/dist/index.js:1:6857)
    at start (/home/alfi/Projects/Hadith-Hub/src/routes/lang/+page.svelte:21:24)
    at eval (/home/alfi/Projects/Hadith-Hub/src/routes/lang/+page.svelte:37:2)
    at Object.$$render (/home/alfi/Projects/Hadith-Hub/node_modules/svelte/src/runtime/internal/ssr.js:174:16)
    at Object.default (/home/alfi/Projects/Hadith-Hub/.svelte-kit/generated/root.svelte:79:138)
    at Object.default (/home/alfi/Projects/Hadith-Hub/src/routes/+layout.svelte:187:44)
    at eval (/home/alfi/Projects/Hadith-Hub/node_modules/@skeletonlabs/skeleton/dist/components/AppShell/AppShell.svelte:58:145)
    at Object.$$render (/home/alfi/Projects/Hadith-Hub/node_modules/svelte/src/runtime/internal/ssr.js:174:16)
    at eval (/home/alfi/Projects/Hadith-Hub/src/routes/+layout.svelte:150:206)
    at Object.$$render (/home/alfi/Projects/Hadith-Hub/node_modules/svelte/src/runtime/internal/ssr.js:174:16)

I am kinda new to this, any help is appreciated.

@mysterybear
Copy link

mysterybear commented Apr 23, 2024

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 new SharedWorker instead of new Worker seems to resolve the issue for me? O.o

Edit edit: ah I don't want to use a SharedWorker so that's not ideal D:

Edit edit edit: I was checking window within some function in my code, this was causing my problem (even checking if window undefined from my worker) sorry for the noise, my thing is resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants