From 8cfa97456bee039ba7d0870c6bc242f507e502b7 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 24 Jun 2022 20:04:23 +0900 Subject: [PATCH] fix(optimizer): always queue run at least once --- packages/vite/src/node/optimizer/index.ts | 1 + packages/vite/src/node/optimizer/optimizer.ts | 15 ++++++++++++--- packages/vite/src/node/plugins/optimizedDeps.ts | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index d9b5d4897d667e..9a4ba48bb1b3a3 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -58,6 +58,7 @@ export interface DepsOptimizer { isOptimizedDepUrl: (url: string) => boolean getOptimizedDepId: (depInfo: OptimizedDepInfo) => string + queueFirstOptimizerRun: () => void delayDepsOptimizerUntil: (id: string, done: () => Promise) => void registerWorkersSource: (id: string) => void resetRegisteredIds: () => void diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index 9a331e9db90ab4..7e3effc2d19178 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -47,6 +47,9 @@ export function getDepsOptimizer( return depsOptimizerMap.get(config.mainConfig || config) } +const dummyId = Symbol('dummy id for first optimizer run') +type DummyId = typeof dummyId + export async function initDepsOptimizer( config: ResolvedConfig, server?: ViteDevServer @@ -81,6 +84,7 @@ export async function initDepsOptimizer( getOptimizedDepId: (depInfo: OptimizedDepInfo) => isBuild ? depInfo.file : `${depInfo.file}?v=${depInfo.browserHash}`, registerWorkersSource, + queueFirstOptimizerRun, delayDepsOptimizerUntil, resetRegisteredIds, options: config.optimizeDeps @@ -517,10 +521,10 @@ export async function initDepsOptimizer( const runOptimizerIfIdleAfterMs = 100 - let registeredIds: { id: string; done: () => Promise }[] = [] + let registeredIds: { id: string | DummyId; done: () => Promise }[] = [] let seenIds = new Set() let workersSources = new Set() - let waitingOn: string | undefined + let waitingOn: string | DummyId | undefined function resetRegisteredIds() { registeredIds = [] @@ -529,6 +533,11 @@ export async function initDepsOptimizer( waitingOn = undefined } + function queueFirstOptimizerRun() { + registeredIds.push({ id: dummyId, done: async () => {} }) + runOptimizerWhenIdle() + } + function registerWorkersSource(id: string): void { workersSources.add(id) // Avoid waiting for this id, as it may be blocked by the rollup @@ -559,7 +568,7 @@ export async function initDepsOptimizer( waitingOn = next.id const afterLoad = () => { waitingOn = undefined - if (!workersSources.has(next.id)) { + if (next.id === dummyId || !workersSources.has(next.id)) { if (registeredIds.length > 0) { runOptimizerWhenIdle() } else { diff --git a/packages/vite/src/node/plugins/optimizedDeps.ts b/packages/vite/src/node/plugins/optimizedDeps.ts index 3eff36bd1bc13e..bd120971b0292a 100644 --- a/packages/vite/src/node/plugins/optimizedDeps.ts +++ b/packages/vite/src/node/plugins/optimizedDeps.ts @@ -86,10 +86,11 @@ export function optimizedDepsBuildPlugin(config: ResolvedConfig): Plugin { buildStart() { if (!config.isWorker) { getDepsOptimizer(config)?.resetRegisteredIds() + getDepsOptimizer(config)?.queueFirstOptimizerRun() } }, - async resolveId(id) { + resolveId(id) { if (getDepsOptimizer(config)?.isOptimizedDepFile(id)) { return id }