Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/config/worker-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Unless noted, the options in this section are applied to all dev, build, and pre

## worker.format

- **Type:** `'es' | 'iife'`
- **Type:** `'es' | 'iife' | 'cjs'`
- **Default:** `'iife'`

Output format for worker bundle.
Output format for worker bundle. When using [`?nodeWorker`](/guide/features#node-worker-imports), prefer `'es'` or `'cjs'`. Other formats will be coerced to `'cjs'` for Node worker builds.

## worker.plugins

Expand Down
17 changes: 17 additions & 0 deletions docs/guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,23 @@ import 'vite/client'
import MyWorker from './worker?worker&url'
```

#### Node Worker Imports

When targeting Node.js worker threads, append the `?nodeWorker` query. The default export is a factory that returns a [`Worker`](https://nodejs.org/api/worker_threads.html#class-worker) from `node:worker_threads`, so it can be used directly in server-side code during development and after build:

```ts twoslash
import createNodeWorker from './worker?nodeWorker'

const worker = createNodeWorker()
worker.postMessage('ping')
worker.on('message', (value) => {
console.log(value)
worker.terminate()
})
```

The same modifiers as web workers are supported. For example, `?nodeWorker&inline` inlines the worker source and runs it with `eval: true`. Node workers currently support `worker.format` values of `'es'` and `'cjs'`; when another format is configured Vite will fall back to `'cjs'`.

See [Worker Options](/config/worker-options.md) for details on configuring the bundling of all workers.

## Content Security Policy (CSP)
Expand Down
14 changes: 14 additions & 0 deletions packages/vite/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ declare module '*?worker&url' {
export default src
}

declare module '*?nodeWorker' {
const workerFactory: (
options?: import('node:worker_threads').WorkerOptions,
) => import('node:worker_threads').Worker
export default workerFactory
}

declare module '*?nodeWorker&inline' {
const workerFactory: (
options?: import('node:worker_threads').WorkerOptions,
) => import('node:worker_threads').Worker
export default workerFactory
}

declare module '*?sharedworker' {
const sharedWorkerConstructor: {
new (options?: { name?: string }): SharedWorker
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export interface UserConfig extends DefaultEnvironmentOptions {
* Output format for worker bundle
* @default 'iife'
*/
format?: 'es' | 'iife'
format?: 'es' | 'iife' | 'cjs'
/**
* Vite plugins that apply to worker bundle. The plugins returned by this function
* should be new instances every time it is called, because they are used for each
Expand Down Expand Up @@ -536,7 +536,7 @@ export interface LegacyOptions {
}

export interface ResolvedWorkerOptions {
format: 'es' | 'iife'
format: 'es' | 'iife' | 'cjs'
plugins: (bundleChain: string[]) => Promise<ResolvedConfig>
rollupOptions: RollupOptions
}
Expand Down Expand Up @@ -1437,6 +1437,7 @@ export async function resolveConfig(
const workerResolved: ResolvedConfig = {
...workerConfig,
...resolved,
command: 'build',
isWorker: true,
mainConfig: resolved,
bundleChain,
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const environment = this.environment as DevEnvironment
const ssr = environment.config.consumer === 'server'
const moduleGraph = environment.moduleGraph
if (!moduleGraph) {
return source
}

if (canSkipImportAnalysis(importer)) {
debug?.(colors.dim(`[skipped] ${prettifyUrl(importer, root)}`))
Expand Down
Loading
Loading