Skip to content

Commit

Permalink
perf(moduleGraph): resolve dep urls in parallel (#12619)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Mar 29, 2023
1 parent f4e2fdf commit 4823fec
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,33 @@ export class ModuleGraph {
): Promise<Set<ModuleNode> | undefined> {
mod.isSelfAccepting = isSelfAccepting
const prevImports = mod.importedModules
const nextImports = (mod.importedModules = new Set())
let noLongerImported: Set<ModuleNode> | undefined

let resolvePromises = []
let resolveResults = new Array(importedModules.size)
let index = 0
// update import graph
for (const imported of importedModules) {
const dep =
typeof imported === 'string'
? await this.ensureEntryFromUrl(imported, ssr)
: imported
dep.importers.add(mod)
nextImports.add(dep)
const nextIndex = index++
if (typeof imported === 'string') {
resolvePromises.push(
this.ensureEntryFromUrl(imported, ssr).then((dep) => {
dep.importers.add(mod)
resolveResults[nextIndex] = dep
}),
)
} else {
imported.importers.add(mod)
resolveResults[nextIndex] = imported
}
}

if (resolvePromises.length) {
await Promise.all(resolvePromises)
}

const nextImports = (mod.importedModules = new Set(resolveResults))

// remove the importer from deps that were imported but no longer are.
prevImports.forEach((dep) => {
if (!nextImports.has(dep)) {
Expand All @@ -169,15 +185,30 @@ export class ModuleGraph {
}
}
})

// update accepted hmr deps
const deps = (mod.acceptedHmrDeps = new Set())
resolvePromises = []
resolveResults = new Array(acceptedModules.size)
index = 0
for (const accepted of acceptedModules) {
const dep =
typeof accepted === 'string'
? await this.ensureEntryFromUrl(accepted, ssr)
: accepted
deps.add(dep)
const nextIndex = index++
if (typeof accepted === 'string') {
resolvePromises.push(
this.ensureEntryFromUrl(accepted, ssr).then((dep) => {
resolveResults[nextIndex] = dep
}),
)
} else {
resolveResults[nextIndex] = accepted
}
}

if (resolvePromises.length) {
await Promise.all(resolvePromises)
}

mod.acceptedHmrDeps = new Set(resolveResults)

// update accepted hmr exports
mod.acceptedHmrExports = acceptedExports
mod.importedBindings = importedBindings
Expand Down

0 comments on commit 4823fec

Please sign in to comment.