-
-
Notifications
You must be signed in to change notification settings - Fork 273
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
I encounter an unknown error when creating ParallelMeshBVHWorker in a Vite environment. #636
Comments
Hello! This syntax is the recommended way to create web workers in Vite. From the docs on web worker support: Can you please confirm that this is not working and help understand what the issue is exactly if it's not? |
I suspect the problem is caused by the Worker, but I’m not sure. I have created a reproducible example. |
I think it would be best to make a simplified example that doesn't include any plugins like Vue so it's more clear that it's caused by the Worker and Vite. If it continues to happen I think this should be raised and reported as a bug to Vite before changes are made to this project. The Vite documentation states that it supports the syntax used in this project so it should be working. |
Just wanted to add some more context for anyone else landing on this via Google. I think this is just that Vite has not implemented support for web workers in their "dependency pre-bundling" step, and the optimizer tries to optimize For this snippet: import { GenerateMeshBVHWorker } from "three-mesh-bvh/src/workers/GenerateMeshBVHWorker.js";
const worker = new GenerateMeshBVHWorker(); and the most minimal Vite setup there can be, running
The worker is indeed missing from The recommended solution in another Vite issue of excluding the files which load the // vite.config.js:
import { defineConfig } from "vite";
export default defineConfig({
optimizeDeps: {
exclude: [
"three-mesh-bvh/src/workers/GenerateMeshBVHWorker.js",
"three-mesh-bvh/src/workers/ParallelMeshBVHWorker.js",
],
},
}); also This theory is supported by the fact that if I just copy the source of import { WorkerBase } from "three-mesh-bvh/src/workers/utils/WorkerBase.js";
import generateMeshBVHWorker from "three-mesh-bvh/src/workers/generateMeshBVH.worker.js?worker";
export class GenerateMeshBVHWorker extends WorkerBase {
constructor() {
super(new generateMeshBVHWorker());
this.name = "GenerateMeshBVHWorker";
}
// ... So I agree with your conclusion - Vite issue. Although, I also do think that being able to optionally inject the worker via the constructor could be nice. That way users can just refer to their toolchain's documentation on how to load workers. P.S. Thanks so much for creating this package! I can't believe how fast raycasts are with a proper index. |
If there's a workaround in Vite right now I'd prefer to hold off on modifying APIs to accommodate bundler quirks. This is valid javascript and is something that works in Webpack 5 & Parcel - so as I see it this is something that should be fixed in bundlers. Though unfortunately Rollup & ESLint still don't have support, either (see rollup/rollup#3842, evanw/esbuild#312). If we see further complaints we can add the change if someone wants to make a PR with the constructor arguments . |
Describe the bug
When creating ParallelMeshBVHWorker in a Vite environment, I encountered an unknown error. Later, I found that the same error occurs with GenerateMeshBVHWorker. I investigated the issue and realized that it might be caused by the way the worker is created using
new Worker( new URL( './generateMeshBVH.worker.js', import.meta.url ), { type: 'module' } )
. So, I modified the code as shown below, and it started working fine. The worker is passed in from outside.
Code
I wonder if it’s possible to add an optional worker parameter to the constructors of GenerateMeshBVHWorker and ParallelMeshBVHWorker to support it. Are there any alternative ways to resolve this issue in a Vite environment?
The text was updated successfully, but these errors were encountered: