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

Nested workers are not emitted during build #5699

Closed
7 tasks done
Floriszenz opened this issue Nov 16, 2021 · 2 comments · Fixed by #6599
Closed
7 tasks done

Nested workers are not emitted during build #5699

Floriszenz opened this issue Nov 16, 2021 · 2 comments · Fixed by #6599

Comments

@Floriszenz
Copy link

Describe the bug

I was working on a library project that uses web workers - a main worker to move computations off the main thread, which can spawn multiple sub workers to distribute computations and run them in parallel. In order to import the workers I used the corresponding syntax (import MyWorker from "./worker?worker";) Vite provides. But when building the library with vite build there is no file for the sub worker emitted.

As you will see in the reproduction section, the same applies for an application as well, so it's not an issue with the library mode.
The application works fine during development but when running vite build the following files are emitted:

dist
├── assets
│   ├── favicon.17e50649.svg
│   ├── index.06d14ce2.css
│   ├── index.b9fb646d.js
│   └── main-worker.71484cf4.js
└── index.html

But in the main-worker.71484cf4.js file the sub worker file is referenced properly:

(()=>{(function(){"use strict";function a(){return new Worker("/assets/sub-worker.e58a1032.js",{type:"module"})}const s=new a;self.onmessage=e=>{e.data==="calculate"&&s.postMessage("calculate")},s.onmessage=e=>{self.postMessage(e.data)}})();})();

So, apparently the sub worker file is included as build artifact but it is just not emitted to the assets directory.

Thanks for your help!

Reproduction

https://github.com/XR4Z0R/vite-nested-workers

The application uses the standard vanilla-ts template and is extended by the main-worker.ts and sub-worker.ts files.
So you can run npm run dev, which should work as expected. And you can run npm run build & npm run serve to preview the production code, which should not work.

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (20) x64 Intel(R) Core(TM) i9-10900K CPU @ 3.70GHz
    Memory: 27.44 GB / 31.30 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.0/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
npmPackages:
    vite: ^2.6.14 => 2.6.14

Used Package Manager

npm

Logs

No response

Validations

@sgenoud
Copy link

sgenoud commented Nov 23, 2021

I have faced a similar bug in my sample app.

I import a wasm file url. The wasm file is linked correctly, but not included in the build.

As a workaround, I imported the wasm asset in the main thread. If you want to reproduce the bug, you will need to comment this line.

@Floriszenz
Copy link
Author

Small update: I've updated Vite in the reproduction repo to 2.8.0-beta.1 because I thought this issue might be related to #6191 and fixed by #6351.
But the issue remains. While import statements like import "./test"; or import something from "./test"; are bundled correctly when used in a worker, a sub worker module as in this case is still not emitted during build.

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

Successfully merging a pull request may close this issue.

3 participants