Skip to content

Commit

Permalink
refactor: simplify id registration during build
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 15, 2023
1 parent 72be105 commit 969ff9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
24 changes: 21 additions & 3 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,12 @@ async function createDepsOptimizer(
crawlEndFinder?.registerWorkersSource(id)
}
function delayDepsOptimizerUntil(id: string, done: () => Promise<any>) {
if (crawlEndFinder && !depsOptimizer.isOptimizedDepFile(id)) {
crawlEndFinder.delayDepsOptimizerUntil(id, done)
if (crawlEndFinder) {
if (depsOptimizer.isOptimizedDepFile(id)) {
crawlEndFinder.ensureFirstRun()
} else {
crawlEndFinder.delayDepsOptimizerUntil(id, done)
}
}
}
}
Expand All @@ -712,6 +716,7 @@ const callCrawlEndIfIdleAfterMs = 50
interface CrawlEndFinder {
registerWorkersSource: (id: string) => void
delayDepsOptimizerUntil: (id: string, done: () => Promise<any>) => void
ensureFirstRun: () => void
cancel: () => void
}

Expand All @@ -720,13 +725,13 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder {
const seenIds = new Set<string>()
const workersSources = new Set<string>()
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
Expand Down Expand Up @@ -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

Expand All @@ -777,6 +794,7 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder {
return {
registerWorkersSource,
delayDepsOptimizerUntil,
ensureFirstRun,
cancel,
}
}
Expand Down
30 changes: 10 additions & 20 deletions packages/vite/src/node/plugins/optimizedDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 969ff9f

Please sign in to comment.