-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: support node workers out of the box #3932
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
Conversation
|
Can you explain a bit about the use case of this feature? |
|
Hi @antfu Assume following code // worker.js import { parentPort } from "worker_thread"
parentPort.on("message", () => {
// some heavy work in thread
...
parentPort.postMessage("result")
}// main.js import MyWorker from "./worker.js?worker"
const worker = new MyWorker()
worker .postMessage("some data");
worker .on("message", result => {
// get result here
console.log(result)
}The current "Vite" version assumes Worker class on window Problems with Node
Solutions
This will let us use worker for node js without hassle. |
|
Would like to know the scenario why would you want to bundle your node app using Vite? Electron or some SSR? |
|
@antfu I am using this for the electron app in typescript having some heavy ML work on worker threads. |
|
I think the scenario makes sense. @prathameshnetake Can you add some tests for this PR? Thanks |
|
@antfu Sure. I will make the test asap. Thanks |
|
@antfu Added tests. Please have a look. Thanks |
|
Is this PR dead? |
|
I will check if the intent of this PR is already in there |
|
Has anyone developed a good workaround for this? Trying to create a library that works both on browser and Node—but this remains a persistent challenge. |
|
Any updates for this PR? |
|
I'm not sure if this PR is still being considered, but I've come across a relevant use-case where the lack of support for worker_threads in Vite and Vitest is becoming a bottleneck. I'm using Vitest to test a Node package. Vitest defaults to 'node' as the environment ( https://vitest.dev/config/#environment My code, which is successfully built using SWC, fails during the testing phase in Vitest. The error message reads "Cannot find module," although the path specified is correct. Here's a simplified snippet: import { Worker } from 'node:worker_threads'
// workerScript.ts exists in the correct location
const worker = new Worker(new URL('./workerScript.js', import.meta.url))The lack of support for worker_threads means that Vite projects cannot use worker_threads in their SSR apps. As a side effect, it also means that Vitest users cannot test code that uses worker_threads. As a temporary measure, I've managed to circumvent the issue by pre-compiling my code and then running the tests against the output in the |
|
I want to use Vite library mode to bundle a node target package with worker pool, so it means Vite still not support for now? 😂 |
|
Maybe try using the plugin directly? Just like electron-vite does https://github.com/alex8088/electron-vite/blob/master/src/plugins/worker.ts |
|
I think it's because Vite is primarily focused on frontend development. However, I wish Vite could support
This can be used during Vite development, but it cannot be executed properly with the |
@Shyam-Chen Did you ever find a better solution to these issues? I'm just now stumbling across these as well. I'm trying to utilize |
@paynecodes My current solution is to use |
|
Will this ever land in Vite? I'm finding this feature to be more and more essential as I use |
|
I'd really like to see this land in vite. If any maintainers are hovering around, could you pop in a quick comment about why we can't have our nice things? |
electron-vite has a `?modulePath` vite plugin that compiles the import and returns an absolute path, so we don't need to use tsx to compile and execute the typescript directly. Vanilla vite has a `?worker` plugin but that's only for webworkers; support for node workers is stalled (vitejs/vite#3932).
electron-vite has a `?modulePath` vite plugin that compiles the import and returns an absolute path, so we don't need to use tsx to compile and execute the typescript directly. Vanilla vite has a `?worker` plugin but that's only for webworkers; support for node workers is stalled (vitejs/vite#3932).
|
We discussed this topic in a recent meeting. While we think this is a nice feature and welcome a PR, these are feedback about the API and implementation:
I'm not sure how dev implementation would looks like, but Vitest's worker emulation https://github.com/vitest-dev/vitest/tree/main/packages/web-worker might provide some hint. It spawns an independent module runner to execute worker code. |
|
Thanks. I can work on the given suggestions and will update the PR accordingly @hi-ogawa |
|
@prathameshnetake Do you need sponsoring to work on this feature? FYI : https://github.com/aheissenberger/vite-plugin-node-worker |
|
I would love to see support land for node worker threads land in Vite! I’ve tried all of the plugin examples, but not of them worked for me. |
|
Per @aviallon's comment, I tried https://github.com/aheissenberger/vite-plugin-node-worker, and it works, as long as I pin to version 1.0.5. Looking forward to proper native support! |
|
Yes, now that SvelteKit and other full stack frameworks start getting some traction, this is slowly becoming more and more of an issue. |
Description
Additional context
This need basepath to be unset in config if target is node
What is the purpose of this pull request?