https://github.com/aheissenberger/vite-plugin-node-worker
Vite plugin for Node.js worker threads. Supports DEV and BUILD modes.
https://github.com/aheissenberger/vite-plugin-node-worker
nodejs plugin vitejs worker-threads
Last synced: 3 months ago
JSON representation
Vite plugin for Node.js worker threads. Supports DEV and BUILD modes.
- Host: GitHub
- URL: https://github.com/aheissenberger/vite-plugin-node-worker
- Owner: aheissenberger
- License: mit
- Created: 2025-08-26T00:24:41.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2025-08-28T00:32:58.000Z (5 months ago)
- Last Synced: 2025-10-04T07:36:14.672Z (3 months ago)
- Topics: nodejs, plugin, vitejs, worker-threads
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-node-worker
A plugin to serve and build ViteJS projects with [Node Worker Threads](https://nodejs.org/api/worker_threads.html).
## Features
* Supports DEV and BUILD mode
* Transforms Workers writen in TypeScript.
* respects alias configuration in vite.config.ts
## Install & Configuration
### Install dependencies
```bash
npm install vite-plugin-node-worker
```
### Setup ViteJS
```ts
// ./vite.config.ts
import { defineConfig } from "vite";
import workerPlugin from "vite-plugin-node-worker";
export default defineConfig({
plugins: [workerPlugin()],
worker: {
plugins: () => [workerPlugin()],
},
});
```
**Note:** `worker: {}` applys the transformation in ViteJS Dev Mode.
### Import Worker
The path to the worker file needs to be suffixed with `?nodeWorker` or `?modulePath`.
**Wrapper Mode**
```ts
import ApiWorker from "./api-worker.ts?nodeWorker";
const apiW = ApiWorker({ workerData: { hello: "world" } });
api.postMessage("MSG")
```
transforms `import ApiWorker from "./api-worker.ts?nodeWorker";` to:
```js
import { Worker } from 'node:worker_threads';
export default function (options) { return new Worker(new URL(${assetRefId}, import.meta.url), options) }
```
**Path Export**
```ts
import ApiWorkerPath from "./api-worker.ts?modulePath";
import { Worker } from "node:worker_threads"
const apiW = new Worker(workerPath, { workerData: { hello: "world" } });
api.postMessage("MSG")
```
transforms `import ApiWorkerPath from "./api-worker.ts?modulePath";` to:
```js
export default ${assetRefId}
```
## Troubleshooting
**Clean Cache:**
```bash
rm -rf node_modules/.vite-plugin-node-worker
```
**Activate Debug**
```bash
VNW_DEBUG=1 npm run dev
```
**Worker Naming Clash**
By default vite will place the worker in the root of the build target directory.
If two worker have the same name they will be overwriten. Use different names or config vite to add a file hash to worker files.
## Related Projects
This plugin is based on code from:
- https://www.npmjs.com/package/@fetsorn/vite-node-worker
- https://github.com/alex8088/electron-vite/blob/master/src/plugins/worker.ts
The difference is the support of the ViteJS DEV mode.
## License
[MIT](LICENSE)