From 969ff9f32f620d6d18bf246b9e517035fb06928e Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 15 Apr 2023 08:07:36 +0200 Subject: [PATCH] refactor: simplify id registration during build --- packages/vite/src/node/optimizer/optimizer.ts | 24 +++++++++++++-- .../vite/src/node/plugins/optimizedDeps.ts | 30 +++++++------------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index c7c0a0f5d766b6..fb26715659a769 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -701,8 +701,12 @@ async function createDepsOptimizer( crawlEndFinder?.registerWorkersSource(id) } function delayDepsOptimizerUntil(id: string, done: () => Promise) { - if (crawlEndFinder && !depsOptimizer.isOptimizedDepFile(id)) { - crawlEndFinder.delayDepsOptimizerUntil(id, done) + if (crawlEndFinder) { + if (depsOptimizer.isOptimizedDepFile(id)) { + crawlEndFinder.ensureFirstRun() + } else { + crawlEndFinder.delayDepsOptimizerUntil(id, done) + } } } } @@ -712,6 +716,7 @@ const callCrawlEndIfIdleAfterMs = 50 interface CrawlEndFinder { registerWorkersSource: (id: string) => void delayDepsOptimizerUntil: (id: string, done: () => Promise) => void + ensureFirstRun: () => void cancel: () => void } @@ -720,13 +725,13 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder { const seenIds = new Set() const workersSources = new Set() let timeoutHandle: NodeJS.Timeout | undefined - let crawlEndCalled = false let cancelled = false function cancel() { cancelled = true } + let crawlEndCalled = false function callOnCrawlEnd() { if (!cancelled && !crawlEndCalled) { crawlEndCalled = true @@ -760,6 +765,18 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder { checkIfCrawlEndAfterTimeout() } + let firstRunEnsured = false + function ensureFirstRun() { + if (!firstRunEnsured && seenIds.size === 0) { + setTimeout(() => { + if (seenIds.size === 0) { + callOnCrawlEnd() + } + }, 200) + } + firstRunEnsured = true + } + function checkIfCrawlEndAfterTimeout() { if (cancelled || registeredIds.size > 0) return @@ -777,6 +794,7 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder { return { registerWorkersSource, delayDepsOptimizerUntil, + ensureFirstRun, cancel, } } diff --git a/packages/vite/src/node/plugins/optimizedDeps.ts b/packages/vite/src/node/plugins/optimizedDeps.ts index 24b886b6a5fe23..4f0595b77a5c3a 100644 --- a/packages/vite/src/node/plugins/optimizedDeps.ts +++ b/packages/vite/src/node/plugins/optimizedDeps.ts @@ -90,33 +90,23 @@ export function optimizedDepsBuildPlugin(config: ResolvedConfig): Plugin { buildStartCalled = true }, - async resolveId(id, importer, options) { + async resolveId(id) { const depsOptimizer = getDepsOptimizer(config) - if (!depsOptimizer) return - - if (depsOptimizer.isOptimizedDepFile(id)) { + if (depsOptimizer?.isOptimizedDepFile(id)) { return id - } else { - if (options?.custom?.['vite:pre-alias']) { - // Skip registering the id if it is being resolved from the pre-alias plugin - // When a optimized dep is aliased, we need to avoid waiting for it before optimizing - return - } - const resolved = await this.resolve(id, importer, { - ...options, - skipSelf: true, - }) - if (resolved) { - depsOptimizer.delayDepsOptimizerUntil(resolved.id, async () => { - await this.load(resolved) - }) - } } }, async load(id) { const depsOptimizer = getDepsOptimizer(config) - if (!depsOptimizer?.isOptimizedDepFile(id)) { + + if (!depsOptimizer) return + + depsOptimizer.delayDepsOptimizerUntil(id, async () => { + await this.load({ id }) + }) + + if (!depsOptimizer.isOptimizedDepFile(id)) { return }