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

refactor(optimizer): await depsOptimizer.scanProcessing #11251

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 40 additions & 47 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,12 @@ async function createDepsOptimizer(
let firstRunCalled = !!cachedMetadata

let postScanOptimizationResult: Promise<DepOptimizationResult> | undefined
let discoverProjectDependenciesPromise:
| Promise<Record<string, string>>
| undefined

let optimizingNewDeps: Promise<DepOptimizationResult> | undefined
async function close() {
closed = true
await Promise.allSettled([
discoverProjectDependenciesPromise?.catch(() => {
/* ignore error for scanner because it's not important */
}),
depsOptimizer.scanProcessing,
postScanOptimizationResult,
optimizingNewDeps,
])
Expand Down Expand Up @@ -203,52 +198,50 @@ async function createDepsOptimizer(

if (!isBuild) {
// Important, the scanner is dev only
const scanPhaseProcessing = newDepOptimizationProcessing()
depsOptimizer.scanProcessing = scanPhaseProcessing.promise
// Ensure server listen is called before the scanner
setTimeout(async () => {
try {
debug(colors.green(`scanning for dependencies...`))

discoverProjectDependenciesPromise =
discoverProjectDependencies(config)
const deps = await discoverProjectDependenciesPromise

debug(
colors.green(
Object.keys(deps).length > 0
? `dependencies found by scanner: ${depsLogString(
Object.keys(deps),
)}`
: `no dependencies found by scanner`,
),
)
depsOptimizer.scanProcessing = new Promise((resolve) => {
// Ensure server listen is called before the scanner
setTimeout(async () => {
try {
debug(colors.green(`scanning for dependencies...`))

const deps = await discoverProjectDependencies(config)

debug(
colors.green(
Object.keys(deps).length > 0
? `dependencies found by scanner: ${depsLogString(
Object.keys(deps),
)}`
: `no dependencies found by scanner`,
),
)

// Add these dependencies to the discovered list, as these are currently
// used by the preAliasPlugin to support aliased and optimized deps.
// This is also used by the CJS externalization heuristics in legacy mode
for (const id of Object.keys(deps)) {
if (!metadata.discovered[id]) {
addMissingDep(id, deps[id])
// Add these dependencies to the discovered list, as these are currently
// used by the preAliasPlugin to support aliased and optimized deps.
// This is also used by the CJS externalization heuristics in legacy mode
for (const id of Object.keys(deps)) {
if (!metadata.discovered[id]) {
addMissingDep(id, deps[id])
}
}
}

if (!isBuild) {
const knownDeps = prepareKnownDeps()
if (!isBuild) {
const knownDeps = prepareKnownDeps()

// For dev, we run the scanner and the first optimization
// run on the background, but we wait until crawling has ended
// to decide if we send this result to the browser or we need to
// do another optimize step
postScanOptimizationResult = runOptimizeDeps(config, knownDeps)
// For dev, we run the scanner and the first optimization
// run on the background, but we wait until crawling has ended
// to decide if we send this result to the browser or we need to
// do another optimize step
postScanOptimizationResult = runOptimizeDeps(config, knownDeps)
}
} catch (e) {
logger.error(e.message)
} finally {
resolve()
depsOptimizer.scanProcessing = undefined
}
} catch (e) {
logger.error(e.message)
} finally {
scanPhaseProcessing.resolve()
depsOptimizer.scanProcessing = undefined
}
}, 0)
}, 0)
})
}
}

Expand Down