Skip to content

Commit 82137d6

Browse files
authoredMar 30, 2023
perf: shorcircuit resolve in ensure entry from url (#12655)
1 parent c268cfa commit 82137d6

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed
 

‎packages/vite/src/node/plugins/importAnalysis.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
391391
// up-to-date version of this module.
392392
try {
393393
// delay setting `isSelfAccepting` until the file is actually used (#7870)
394-
const depModule = await moduleGraph.ensureEntryFromUrl(
394+
// We use an internal function to avoid resolving the url again
395+
const depModule = await moduleGraph._ensureEntryFromUrl(
395396
unwrapId(url),
396397
ssr,
397398
canSkipImportAnalysis(url) || forceSkipImportAnalysis,
399+
resolved,
398400
)
399401
if (depModule.lastHMRTimestamp > 0) {
400402
url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`)

‎packages/vite/src/node/server/moduleGraph.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,31 @@ export class ModuleGraph {
235235
rawUrl: string,
236236
ssr?: boolean,
237237
setIsSelfAccepting = true,
238+
): Promise<ModuleNode> {
239+
return this._ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting)
240+
}
241+
242+
/**
243+
* @internal
244+
*/
245+
async _ensureEntryFromUrl(
246+
rawUrl: string,
247+
ssr?: boolean,
248+
setIsSelfAccepting = true,
249+
// Optimization, avoid resolving the same url twice if the caller already did it
250+
resolved?: PartialResolvedId,
238251
): Promise<ModuleNode> {
239252
// Quick path, if we already have a module for this rawUrl (even without extension)
240253
rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
241254
let mod = this._getUnresolvedUrlToModule(rawUrl, ssr)
242255
if (mod) {
243256
return mod
244257
}
245-
246-
const [url, resolvedId, meta] = await this._resolveUrl(rawUrl, ssr)
258+
const [url, resolvedId, meta] = await this._resolveUrl(
259+
rawUrl,
260+
ssr,
261+
resolved,
262+
)
247263
mod = this.idToModuleMap.get(resolvedId)
248264
if (!mod) {
249265
mod = new ModuleNode(url, setIsSelfAccepting)
@@ -334,8 +350,12 @@ export class ModuleGraph {
334350
/**
335351
* @internal
336352
*/
337-
async _resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
338-
const resolved = await this.resolveId(url, !!ssr)
353+
async _resolveUrl(
354+
url: string,
355+
ssr?: boolean,
356+
alreadyResolved?: PartialResolvedId,
357+
): Promise<ResolvedUrl> {
358+
const resolved = alreadyResolved ?? (await this.resolveId(url, !!ssr))
339359
const resolvedId = resolved?.id || url
340360
if (
341361
url !== resolvedId &&

0 commit comments

Comments
 (0)
Please sign in to comment.