Skip to content

Commit 306bed0

Browse files
authored
fix: serialize bundleWorkerEntry (#11218)
1 parent f24679c commit 306bed0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/vite/src/node/plugins/worker.ts

+23
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,33 @@ function saveEmitWorkerAsset(
4949
workerMap.assets.set(fileName, asset)
5050
}
5151

52+
// Ensure that only one rollup build is called at the same time to avoid
53+
// leaking state in plugins between worker builds.
54+
// TODO: Review if we can parallelize the bundling of workers.
55+
const workerConfigSemaphore = new WeakMap<
56+
ResolvedConfig,
57+
Promise<OutputChunk>
58+
>()
5259
export async function bundleWorkerEntry(
5360
config: ResolvedConfig,
5461
id: string,
5562
query: Record<string, string> | null,
63+
): Promise<OutputChunk> {
64+
const processing = workerConfigSemaphore.get(config)
65+
if (processing) {
66+
await processing
67+
return bundleWorkerEntry(config, id, query)
68+
}
69+
const promise = serialBundleWorkerEntry(config, id, query)
70+
workerConfigSemaphore.set(config, promise)
71+
promise.then(() => workerConfigSemaphore.delete(config))
72+
return promise
73+
}
74+
75+
async function serialBundleWorkerEntry(
76+
config: ResolvedConfig,
77+
id: string,
78+
query: Record<string, string> | null,
5679
): Promise<OutputChunk> {
5780
// bundle the file as entry to support imports
5881
const { rollup } = await import('rollup')

0 commit comments

Comments
 (0)