Skip to content

Commit

Permalink
perf: shorcircuit resolve in ensure entry from url (#12655)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Mar 30, 2023
1 parent c268cfa commit 82137d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// up-to-date version of this module.
try {
// delay setting `isSelfAccepting` until the file is actually used (#7870)
const depModule = await moduleGraph.ensureEntryFromUrl(
// We use an internal function to avoid resolving the url again
const depModule = await moduleGraph._ensureEntryFromUrl(
unwrapId(url),
ssr,
canSkipImportAnalysis(url) || forceSkipImportAnalysis,
resolved,
)
if (depModule.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`)
Expand Down
28 changes: 24 additions & 4 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,31 @@ export class ModuleGraph {
rawUrl: string,
ssr?: boolean,
setIsSelfAccepting = true,
): Promise<ModuleNode> {
return this._ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting)
}

/**
* @internal
*/
async _ensureEntryFromUrl(
rawUrl: string,
ssr?: boolean,
setIsSelfAccepting = true,
// Optimization, avoid resolving the same url twice if the caller already did it
resolved?: PartialResolvedId,
): Promise<ModuleNode> {
// Quick path, if we already have a module for this rawUrl (even without extension)
rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
let mod = this._getUnresolvedUrlToModule(rawUrl, ssr)
if (mod) {
return mod
}

const [url, resolvedId, meta] = await this._resolveUrl(rawUrl, ssr)
const [url, resolvedId, meta] = await this._resolveUrl(
rawUrl,
ssr,
resolved,
)
mod = this.idToModuleMap.get(resolvedId)
if (!mod) {
mod = new ModuleNode(url, setIsSelfAccepting)
Expand Down Expand Up @@ -334,8 +350,12 @@ export class ModuleGraph {
/**
* @internal
*/
async _resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
const resolved = await this.resolveId(url, !!ssr)
async _resolveUrl(
url: string,
ssr?: boolean,
alreadyResolved?: PartialResolvedId,
): Promise<ResolvedUrl> {
const resolved = alreadyResolved ?? (await this.resolveId(url, !!ssr))
const resolvedId = resolved?.id || url
if (
url !== resolvedId &&
Expand Down

0 comments on commit 82137d6

Please sign in to comment.