Skip to content

Commit

Permalink
fix: serialize bundleWorkerEntry (#11218)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Dec 6, 2022
1 parent f24679c commit 306bed0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -49,10 +49,33 @@ function saveEmitWorkerAsset(
workerMap.assets.set(fileName, asset)
}

// Ensure that only one rollup build is called at the same time to avoid
// leaking state in plugins between worker builds.
// TODO: Review if we can parallelize the bundling of workers.
const workerConfigSemaphore = new WeakMap<
ResolvedConfig,
Promise<OutputChunk>
>()
export async function bundleWorkerEntry(
config: ResolvedConfig,
id: string,
query: Record<string, string> | null,
): Promise<OutputChunk> {
const processing = workerConfigSemaphore.get(config)
if (processing) {
await processing
return bundleWorkerEntry(config, id, query)
}
const promise = serialBundleWorkerEntry(config, id, query)
workerConfigSemaphore.set(config, promise)
promise.then(() => workerConfigSemaphore.delete(config))
return promise
}

async function serialBundleWorkerEntry(
config: ResolvedConfig,
id: string,
query: Record<string, string> | null,
): Promise<OutputChunk> {
// bundle the file as entry to support imports
const { rollup } = await import('rollup')
Expand Down

0 comments on commit 306bed0

Please sign in to comment.