diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index 45fea171ddcc1e..4bbd79cd5cc2f6 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -1,5 +1,4 @@ import { extname } from 'node:path' -import { parse as parseUrl } from 'node:url' import type { ModuleInfo, PartialResolvedId } from 'rollup' import { isDirectCSSRequest } from '../plugins/css' import { @@ -237,10 +236,16 @@ export class ModuleGraph { url = removeImportQuery(removeTimestampQuery(url)) const resolved = await this.resolveId(url, !!ssr) const resolvedId = resolved?.id || url - const ext = extname(cleanUrl(resolvedId)) - const { pathname, search, hash } = parseUrl(url) - if (ext && !pathname!.endsWith(ext)) { - url = pathname + ext + (search || '') + (hash || '') + if ( + url !== resolvedId && + !url.includes('\0') && + !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 + } } return [url, resolvedId, resolved?.meta] } diff --git a/playground/resolve/index.html b/playground/resolve/index.html index a1befcc2757348..bca49d55388e27 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -76,6 +76,9 @@