Skip to content

Commit c268cfa

Browse files
authoredMar 30, 2023
perf: module graph url shortcuts (#12635)
1 parent 1f011d8 commit c268cfa

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
694694
const normalizedAcceptedUrls = new Set<string>()
695695
for (const { url, start, end } of acceptedUrls) {
696696
const [normalized] = await moduleGraph.resolveUrl(
697-
toAbsoluteUrl(markExplicitImport(url)),
697+
toAbsoluteUrl(url),
698698
ssr,
699699
)
700700
normalizedAcceptedUrls.add(normalized)

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

+63-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ export class ModuleGraph {
6161
fileToModulesMap = new Map<string, Set<ModuleNode>>()
6262
safeModulesPath = new Set<string>()
6363

64+
/**
65+
* @internal
66+
*/
67+
_unresolvedUrlToModuleMap = new Map<string, ModuleNode>()
68+
/**
69+
* @internal
70+
*/
71+
_ssrUnresolvedUrlToModuleMap = new Map<string, ModuleNode>()
72+
6473
constructor(
6574
private resolveId: (
6675
url: string,
@@ -72,7 +81,14 @@ export class ModuleGraph {
7281
rawUrl: string,
7382
ssr?: boolean,
7483
): Promise<ModuleNode | undefined> {
75-
const [url] = await this.resolveUrl(rawUrl, ssr)
84+
// Quick path, if we already have a module for this rawUrl (even without extension)
85+
rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
86+
const mod = this._getUnresolvedUrlToModule(rawUrl, ssr)
87+
if (mod) {
88+
return mod
89+
}
90+
91+
const [url] = await this._resolveUrl(rawUrl, ssr)
7692
return this.urlToModuleMap.get(url)
7793
}
7894

@@ -220,8 +236,15 @@ export class ModuleGraph {
220236
ssr?: boolean,
221237
setIsSelfAccepting = true,
222238
): Promise<ModuleNode> {
223-
const [url, resolvedId, meta] = await this.resolveUrl(rawUrl, ssr)
224-
let mod = this.idToModuleMap.get(resolvedId)
239+
// Quick path, if we already have a module for this rawUrl (even without extension)
240+
rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
241+
let mod = this._getUnresolvedUrlToModule(rawUrl, ssr)
242+
if (mod) {
243+
return mod
244+
}
245+
246+
const [url, resolvedId, meta] = await this._resolveUrl(rawUrl, ssr)
247+
mod = this.idToModuleMap.get(resolvedId)
225248
if (!mod) {
226249
mod = new ModuleNode(url, setIsSelfAccepting)
227250
if (meta) mod.meta = meta
@@ -241,6 +264,11 @@ export class ModuleGraph {
241264
else if (!this.urlToModuleMap.has(url)) {
242265
this.urlToModuleMap.set(url, mod)
243266
}
267+
268+
// Also register the clean url to the module, so that we can short-circuit
269+
// resolving the same url twice
270+
this._setUnresolvedUrlToModule(rawUrl, mod, ssr)
271+
244272
return mod
245273
}
246274

@@ -275,6 +303,38 @@ export class ModuleGraph {
275303
// the same module
276304
async resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
277305
url = removeImportQuery(removeTimestampQuery(url))
306+
const mod = this._getUnresolvedUrlToModule(url, ssr)
307+
if (mod?.id) {
308+
return [mod.url, mod.id, mod.meta]
309+
}
310+
return this._resolveUrl(url, ssr)
311+
}
312+
313+
/**
314+
* @internal
315+
*/
316+
_getUnresolvedUrlToModule(
317+
url: string,
318+
ssr?: boolean,
319+
): ModuleNode | undefined {
320+
return (
321+
ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap
322+
).get(url)
323+
}
324+
/**
325+
* @internal
326+
*/
327+
_setUnresolvedUrlToModule(url: string, mod: ModuleNode, ssr?: boolean): void {
328+
;(ssr
329+
? this._ssrUnresolvedUrlToModuleMap
330+
: this._unresolvedUrlToModuleMap
331+
).set(url, mod)
332+
}
333+
334+
/**
335+
* @internal
336+
*/
337+
async _resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
278338
const resolved = await this.resolveId(url, !!ssr)
279339
const resolvedId = resolved?.id || url
280340
if (

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ async function doTransform(
144144

145145
// resolve
146146
const id =
147-
(await pluginContainer.resolveId(url, undefined, { ssr }))?.id || url
147+
module?.id ??
148+
(await pluginContainer.resolveId(url, undefined, { ssr }))?.id ??
149+
url
148150

149151
const result = loadAndTransform(id, url, server, options, timestamp)
150152

0 commit comments

Comments
 (0)
Please sign in to comment.