feat: node worker support out of the box#21160
Open
prathameshnetake wants to merge 7 commits into
Open
Conversation
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
4 tasks
|
I'd pay for this to be merged. Seriously. |
Author
|
I’m eager to see this PR (or another that adds worker support) land. It feels like a glaring gap in Vite that it doesn’t support multithreaded Node apps. In case it helps anyone else, in my SvelteKit app I use the following custom plugin to enable worker support; edit as needed for your own worker files and paths: import { copyFile, mkdir, cp } from 'node:fs/promises'
import { resolve } from 'node:path'
import { env } from 'node:process'
/**
* Vite plugin to copy worker files to the build directory after adapter-node finishes.
* This ensures worker threads are available when running in preview or production mode.
*
* vite-plugin-node-worker outputs to .svelte-kit/output/server/, but adapter-node
* doesn't automatically copy worker files to build/server/. This plugin handles that.
* We also need to copy the chunks directory since the worker imports from it.
*/
export function copyWorkerToBuildPlugin() {
return {
name: 'copy-worker-to-build',
/**
* Run after the adapter has finished writing files
*/
async closeBundle() {
const outputDir = resolve(import.meta.dirname, '../../.svelte-kit/output/server')
const sourceWorker = resolve(outputDir, 'my-worker.worker.mjs')
const sourceChunks = resolve(outputDir, 'chunks')
const targetDir = resolve(import.meta.dirname, '../../build/server')
const targetWorker = resolve(targetDir, 'my-worker.worker.mjs')
const targetChunks = resolve(targetDir, 'chunks')
try {
// Ensure target directory exists
await mkdir(targetDir, { recursive: true })
// Copy the worker file
await copyFile(sourceWorker, targetWorker)
// Copy the chunks directory (worker dependencies)
// Use force: false to not overwrite existing chunks from adapter-node
await cp(sourceChunks, targetChunks, { recursive: true, force: false })
} catch (error) {
// Only error if we're in a build context where the worker file should exist
if ((error instanceof Error && 'code' in error && error.code !== 'ENOENT') || env.BUILDING) {
console.error('Failed to copy worker file:', error)
}
}
},
}
} |
|
@antfu Is there something that should be done before it is mergeable? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support building of NodeJS worker out of the box. This PR also support dev mode
Successor of an old PR #3932