Skip to content

Commit

Permalink
perf: avoid new URL() in hot path (#12654)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 29, 2023
1 parent 1953f3f commit f4e2fdf
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/vite/src/node/server/moduleGraph.ts
Expand Up @@ -239,7 +239,7 @@ export class ModuleGraph {
}

// for incoming urls, it is important to:
// 1. remove the HMR timestamp query (?t=xxxx)
// 1. remove the HMR timestamp query (?t=xxxx) and the ?import query
// 2. resolve its extension so that urls with or without extension all map to
// the same module
async resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
Expand All @@ -252,9 +252,11 @@ export class ModuleGraph {
!url.startsWith(`virtual:`)
) {
const ext = extname(cleanUrl(resolvedId))
const { pathname, search, hash } = new URL(url, 'relative://')
if (ext && !pathname!.endsWith(ext)) {
url = pathname + ext + search + hash
if (ext) {
const pathname = cleanUrl(url)
if (!pathname.endsWith(ext)) {
url = pathname + ext + url.slice(pathname.length)
}
}
}
return [url, resolvedId, resolved?.meta]
Expand Down

0 comments on commit f4e2fdf

Please sign in to comment.