diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 891d2dd709833c..145e4728c9c8c4 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -66,6 +66,8 @@ export interface DepsOptimizer { resetRegisteredIds: () => void ensureFirstRun: () => void + close: () => Promise + options: DepOptimizationOptions } diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index 0f32d70300dfb7..37061cdaf0c6f5 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -103,6 +103,8 @@ async function createDepsOptimizer( let handle: NodeJS.Timeout | undefined + let closed = false + let metadata = cachedMetadata || initDepsOptimizerMetadata(config, ssr, sessionTimestamp) @@ -118,6 +120,7 @@ async function createDepsOptimizer( delayDepsOptimizerUntil, resetRegisteredIds, ensureFirstRun, + close, options: getDepOptimizationConfig(config, ssr) } @@ -159,6 +162,13 @@ async function createDepsOptimizer( let postScanOptimizationResult: Promise | undefined + let optimizingNewDeps: Promise | undefined + async function close() { + closed = true + await postScanOptimizationResult + await optimizingNewDeps + } + if (!cachedMetadata) { // Enter processing state until crawl of static imports ends currentlyProcessing = true @@ -288,7 +298,7 @@ async function createDepsOptimizer( // Ensure that a rerun will not be issued for current discovered deps if (handle) clearTimeout(handle) - if (Object.keys(metadata.discovered).length === 0) { + if (closed || Object.keys(metadata.discovered).length === 0) { currentlyProcessing = false return } @@ -296,7 +306,16 @@ async function createDepsOptimizer( currentlyProcessing = true try { - const processingResult = preRunResult ?? (await optimizeNewDeps()) + const processingResult = + preRunResult ?? (await (optimizingNewDeps = optimizeNewDeps())) + optimizingNewDeps = undefined + + if (closed) { + currentlyProcessing = false + processingResult.cancel() + resolveEnqueuedProcessingPromises() + return + } const newData = processingResult.metadata @@ -665,7 +684,7 @@ async function createDepsOptimizer( function ensureFirstRun() { if (!firstRunEnsured && !firstRunCalled && registeredIds.length === 0) { setTimeout(() => { - if (registeredIds.length === 0) { + if (!closed && registeredIds.length === 0) { onCrawlEnd() } }, runOptimizerIfIdleAfterMs) @@ -699,7 +718,7 @@ async function createDepsOptimizer( waitingOn = next.id const afterLoad = () => { waitingOn = undefined - if (!workersSources.has(next.id)) { + if (!closed && !workersSources.has(next.id)) { if (registeredIds.length > 0) { runOptimizerWhenIdle() } else { @@ -745,6 +764,8 @@ async function createDevSsrDepsOptimizer( delayDepsOptimizerUntil: (id: string, done: () => Promise) => {}, resetRegisteredIds: () => {}, ensureFirstRun: () => {}, + + close: async () => {}, options: config.ssr.optimizeDeps } devSsrDepsOptimizerMap.set(config, depsOptimizer) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index f51dfb8b19401d..e2dcfa13734b9c 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -395,6 +395,8 @@ export async function createServer( watcher.close(), ws.close(), container.close(), + getDepsOptimizer(server.config)?.close(), + getDepsOptimizer(server.config, true)?.close(), closeHttpServer() ]) server.resolvedUrls = null