From 3e797be93fdde1ef201924d38cfa187d5303e8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Fri, 13 Oct 2023 11:12:51 +0200 Subject: [PATCH] fix: worker build When we have multiple workers, the config cannot be used as web map key since the config can mutate. Since we're not in a multi-thread environment we can use a fixed key to store the promise of the current worker build. You can use the reproduction provided in https://github.com/vitejs/vite/issues/13367 , when building the third worker, there is a pending promise since the config changes and there is no way to delete it and then the error. --- packages/vite/src/node/plugins/worker.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index c4ff6dff78bb2e..411386f25e8a3e 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -44,6 +44,7 @@ function saveEmitWorkerAsset( // 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 workerKey = { key: 'vite:worker' } const workerConfigSemaphore = new WeakMap< ResolvedConfig, Promise @@ -53,14 +54,14 @@ export async function bundleWorkerEntry( id: string, query: Record | null, ): Promise { - const processing = workerConfigSemaphore.get(config) + const processing = workerConfigSemaphore.get(workerKey) if (processing) { await processing return bundleWorkerEntry(config, id, query) } const promise = serialBundleWorkerEntry(config, id, query) - workerConfigSemaphore.set(config, promise) - promise.then(() => workerConfigSemaphore.delete(config)) + workerConfigSemaphore.set(workerKey, promise) + promise.then(() => workerConfigSemaphore.delete(workerKey)) return promise }