From d56e1385fd1f000b8be6ed5e17d366d5fc6244ee Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 6 Dec 2022 11:35:47 +0100 Subject: [PATCH 1/3] fix: serialize bundleWorkerEntry --- packages/vite/src/node/plugins/worker.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 363b83edfa8d72..036c9f1c66d033 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -49,10 +49,32 @@ 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. +const workerConfigSemaphore = new WeakMap< + ResolvedConfig, + Promise +>() export async function bundleWorkerEntry( config: ResolvedConfig, id: string, query: Record | null, +): Promise { + 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 | null, ): Promise { // bundle the file as entry to support imports const { rollup } = await import('rollup') From f5247a21fab067f61b92edea47f4f98d0e32804d Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 6 Dec 2022 13:13:00 +0100 Subject: [PATCH 2/3] chore: add TODO --- packages/vite/src/node/plugins/worker.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 036c9f1c66d033..8350b68ea60d93 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -50,7 +50,8 @@ function saveEmitWorkerAsset( } // Ensure that only one rollup build is called at the same time to avoid -// leaking state in plugins between worker builds. +// leaking state in plugins between worker builds. +// TODO: Review if we can parallelize the bundling of workers. const workerConfigSemaphore = new WeakMap< ResolvedConfig, Promise From cb2eedea13fb0f5fa35003ca90ae92c51b749413 Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 6 Dec 2022 13:16:51 +0100 Subject: [PATCH 3/3] chore: lint --- packages/vite/src/node/plugins/worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 8350b68ea60d93..acb8c67eb1e88a 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -50,7 +50,7 @@ function saveEmitWorkerAsset( } // Ensure that only one rollup build is called at the same time to avoid -// leaking state in plugins between worker builds. +// leaking state in plugins between worker builds. // TODO: Review if we can parallelize the bundling of workers. const workerConfigSemaphore = new WeakMap< ResolvedConfig,