Skip to content

Commit

Permalink
fix: ensure deps optimizer first run, fixes #8750 (#8775)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Jun 26, 2022
1 parent 4a6408b commit 3f689a4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface DepsOptimizer {
delayDepsOptimizerUntil: (id: string, done: () => Promise<any>) => void
registerWorkersSource: (id: string) => void
resetRegisteredIds: () => void
ensureFirstRun: () => void

options: DepOptimizationOptions
}
Expand Down
19 changes: 18 additions & 1 deletion packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function initDepsOptimizer(
registerWorkersSource,
delayDepsOptimizerUntil,
resetRegisteredIds,
ensureFirstRun,
options: config.optimizeDeps
}

Expand Down Expand Up @@ -521,12 +522,28 @@ export async function initDepsOptimizer(
let seenIds = new Set<string>()
let workersSources = new Set<string>()
let waitingOn: string | undefined
let firstRunEnsured = false

function resetRegisteredIds() {
registeredIds = []
seenIds = new Set<string>()
workersSources = new Set<string>()
waitingOn = undefined
firstRunEnsured = false
}

// If all the inputs are dependencies, we aren't going to get any
// delayDepsOptimizerUntil(id) calls. We need to guard against this
// by forcing a rerun if no deps have been registered
function ensureFirstRun() {
if (!firstRunEnsured && !firstRunCalled && registeredIds.length === 0) {
setTimeout(() => {
if (!firstRunCalled && registeredIds.length === 0) {
getDepsOptimizer(config)?.run()
}
}, runOptimizerIfIdleAfterMs)
}
firstRunEnsured = true
}

function registerWorkersSource(id: string): void {
Expand Down Expand Up @@ -559,7 +576,7 @@ export async function initDepsOptimizer(
waitingOn = next.id
const afterLoad = () => {
waitingOn = undefined
if (!workersSources.has(next.id)) {
if (!firstRunCalled && !workersSources.has(next.id)) {
if (registeredIds.length > 0) {
runOptimizerWhenIdle()
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/plugins/optimizedDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ export function optimizedDepsBuildPlugin(config: ResolvedConfig): Plugin {
if (!metadata || !depsOptimizer?.isOptimizedDepFile(id)) {
return
}

depsOptimizer?.ensureFirstRun()

const file = cleanUrl(id)
// Search in both the currently optimized and newly discovered deps
// If all the inputs are dependencies, we aren't going to get any
const info = optimizedDepInfoFromFile(metadata, file)
if (info) {
try {
Expand Down

0 comments on commit 3f689a4

Please sign in to comment.