Skip to content

Commit fa64c8e

Browse files
authored
refactor(optimizer): await depsOptimizer.scanProcessing (#11251)
1 parent b61894e commit fa64c8e

File tree

1 file changed

+40
-47
lines changed

1 file changed

+40
-47
lines changed

packages/vite/src/node/optimizer/optimizer.ts

+40-47
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,12 @@ async function createDepsOptimizer(
161161
let firstRunCalled = !!cachedMetadata
162162

163163
let postScanOptimizationResult: Promise<DepOptimizationResult> | undefined
164-
let discoverProjectDependenciesPromise:
165-
| Promise<Record<string, string>>
166-
| undefined
167164

168165
let optimizingNewDeps: Promise<DepOptimizationResult> | undefined
169166
async function close() {
170167
closed = true
171168
await Promise.allSettled([
172-
discoverProjectDependenciesPromise?.catch(() => {
173-
/* ignore error for scanner because it's not important */
174-
}),
169+
depsOptimizer.scanProcessing,
175170
postScanOptimizationResult,
176171
optimizingNewDeps,
177172
])
@@ -203,52 +198,50 @@ async function createDepsOptimizer(
203198

204199
if (!isBuild) {
205200
// Important, the scanner is dev only
206-
const scanPhaseProcessing = newDepOptimizationProcessing()
207-
depsOptimizer.scanProcessing = scanPhaseProcessing.promise
208-
// Ensure server listen is called before the scanner
209-
setTimeout(async () => {
210-
try {
211-
debug(colors.green(`scanning for dependencies...`))
212-
213-
discoverProjectDependenciesPromise =
214-
discoverProjectDependencies(config)
215-
const deps = await discoverProjectDependenciesPromise
216-
217-
debug(
218-
colors.green(
219-
Object.keys(deps).length > 0
220-
? `dependencies found by scanner: ${depsLogString(
221-
Object.keys(deps),
222-
)}`
223-
: `no dependencies found by scanner`,
224-
),
225-
)
201+
depsOptimizer.scanProcessing = new Promise((resolve) => {
202+
// Ensure server listen is called before the scanner
203+
setTimeout(async () => {
204+
try {
205+
debug(colors.green(`scanning for dependencies...`))
206+
207+
const deps = await discoverProjectDependencies(config)
208+
209+
debug(
210+
colors.green(
211+
Object.keys(deps).length > 0
212+
? `dependencies found by scanner: ${depsLogString(
213+
Object.keys(deps),
214+
)}`
215+
: `no dependencies found by scanner`,
216+
),
217+
)
226218

227-
// Add these dependencies to the discovered list, as these are currently
228-
// used by the preAliasPlugin to support aliased and optimized deps.
229-
// This is also used by the CJS externalization heuristics in legacy mode
230-
for (const id of Object.keys(deps)) {
231-
if (!metadata.discovered[id]) {
232-
addMissingDep(id, deps[id])
219+
// Add these dependencies to the discovered list, as these are currently
220+
// used by the preAliasPlugin to support aliased and optimized deps.
221+
// This is also used by the CJS externalization heuristics in legacy mode
222+
for (const id of Object.keys(deps)) {
223+
if (!metadata.discovered[id]) {
224+
addMissingDep(id, deps[id])
225+
}
233226
}
234-
}
235227

236-
if (!isBuild) {
237-
const knownDeps = prepareKnownDeps()
228+
if (!isBuild) {
229+
const knownDeps = prepareKnownDeps()
238230

239-
// For dev, we run the scanner and the first optimization
240-
// run on the background, but we wait until crawling has ended
241-
// to decide if we send this result to the browser or we need to
242-
// do another optimize step
243-
postScanOptimizationResult = runOptimizeDeps(config, knownDeps)
231+
// For dev, we run the scanner and the first optimization
232+
// run on the background, but we wait until crawling has ended
233+
// to decide if we send this result to the browser or we need to
234+
// do another optimize step
235+
postScanOptimizationResult = runOptimizeDeps(config, knownDeps)
236+
}
237+
} catch (e) {
238+
logger.error(e.message)
239+
} finally {
240+
resolve()
241+
depsOptimizer.scanProcessing = undefined
244242
}
245-
} catch (e) {
246-
logger.error(e.message)
247-
} finally {
248-
scanPhaseProcessing.resolve()
249-
depsOptimizer.scanProcessing = undefined
250-
}
251-
}, 0)
243+
}, 0)
244+
})
252245
}
253246
}
254247

0 commit comments

Comments
 (0)