Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(optimizer): always queue run at least once (fixes #8750) #8760

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -58,6 +58,7 @@ export interface DepsOptimizer {
isOptimizedDepUrl: (url: string) => boolean
getOptimizedDepId: (depInfo: OptimizedDepInfo) => string

queueFirstOptimizerRun: () => void
delayDepsOptimizerUntil: (id: string, done: () => Promise<any>) => void
registerWorkersSource: (id: string) => void
resetRegisteredIds: () => void
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/node/optimizer/optimizer.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -517,10 +521,10 @@ export async function initDepsOptimizer(

const runOptimizerIfIdleAfterMs = 100

let registeredIds: { id: string; done: () => Promise<any> }[] = []
let registeredIds: { id: string | DummyId; done: () => Promise<any> }[] = []
let seenIds = new Set<string>()
let workersSources = new Set<string>()
let waitingOn: string | undefined
let waitingOn: string | DummyId | undefined

function resetRegisteredIds() {
registeredIds = []
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/optimizedDeps.ts
Expand Up @@ -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
}
Expand Down