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

perf: module graph url shortcuts #12635

Merged
merged 17 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const [normalized] = await moduleGraph.resolveUrl(
toAbsoluteUrl(markExplicitImport(url)),
toAbsoluteUrl(url),
ssr,
)
normalizedAcceptedUrls.add(normalized)
Expand Down
66 changes: 63 additions & 3 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ export class ModuleGraph {
fileToModulesMap = new Map<string, Set<ModuleNode>>()
safeModulesPath = new Set<string>()

/**
* @internal
*/
_unresolvedUrlToModuleMap = new Map<string, ModuleNode>()
/**
* @internal
*/
_ssrUnresolvedUrlToModuleMap = new Map<string, ModuleNode>()

constructor(
private resolveId: (
url: string,
Expand All @@ -72,7 +81,14 @@ export class ModuleGraph {
rawUrl: string,
ssr?: boolean,
): Promise<ModuleNode | undefined> {
const [url] = await this.resolveUrl(rawUrl, ssr)
// Quick path, if we already have a module for this rawUrl (even without extension)
rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
const mod = this._getUnresolvedUrlToModule(rawUrl, ssr)
if (mod) {
return mod
}

const [url] = await this._resolveUrl(rawUrl, ssr)
return this.urlToModuleMap.get(url)
}

Expand Down Expand Up @@ -220,8 +236,15 @@ export class ModuleGraph {
ssr?: boolean,
setIsSelfAccepting = true,
): Promise<ModuleNode> {
const [url, resolvedId, meta] = await this.resolveUrl(rawUrl, ssr)
let mod = this.idToModuleMap.get(resolvedId)
// 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)
mod = this.idToModuleMap.get(resolvedId)
if (!mod) {
mod = new ModuleNode(url, setIsSelfAccepting)
if (meta) mod.meta = meta
Expand All @@ -241,6 +264,11 @@ export class ModuleGraph {
else if (!this.urlToModuleMap.has(url)) {
this.urlToModuleMap.set(url, mod)
}

// Also register the clean url to the module, so that we can short-circuit
// resolving the same url twice
this._setUnresolvedUrlToModule(rawUrl, mod, ssr)

return mod
}

Expand Down Expand Up @@ -275,6 +303,38 @@ export class ModuleGraph {
// the same module
async resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
url = removeImportQuery(removeTimestampQuery(url))
const mod = this._getUnresolvedUrlToModule(url, ssr)
if (mod?.id) {
return [mod.url, mod.id, mod.meta]
}
return this._resolveUrl(url, ssr)
}

/**
* @internal
*/
_getUnresolvedUrlToModule(
url: string,
ssr?: boolean,
): ModuleNode | undefined {
return (
ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap
).get(url)
}
/**
* @internal
*/
_setUnresolvedUrlToModule(url: string, mod: ModuleNode, ssr?: boolean): void {
;(ssr
? this._ssrUnresolvedUrlToModuleMap
: this._unresolvedUrlToModuleMap
).set(url, mod)
}

/**
* @internal
*/
async _resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
const resolved = await this.resolveId(url, !!ssr)
const resolvedId = resolved?.id || url
if (
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ async function doTransform(

// resolve
const id =
(await pluginContainer.resolveId(url, undefined, { ssr }))?.id || url
module?.id ??
(await pluginContainer.resolveId(url, undefined, { ssr }))?.id ??
url

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

Expand Down