From f4e2fdf069a46378122d10111afbc669ae3a3874 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 29 Mar 2023 21:19:19 +0200 Subject: [PATCH] perf: avoid new URL() in hot path (#12654) --- packages/vite/src/node/server/moduleGraph.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index 4db3bcbca9a0f4..434d89b52d2cdc 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -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 { @@ -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]