Skip to content

Commit a5412f8

Browse files
authored
fix: Improve injectQuery path handling (#2435)
close #2422
1 parent 9243cc9 commit a5412f8

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/vite/src/node/utils.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from 'chalk'
33
import fs from 'fs'
44
import os from 'os'
55
import path from 'path'
6-
import { parse as parseUrl } from 'url'
6+
import { pathToFileURL, URL } from 'url'
77
import { FS_PREFIX, DEFAULT_EXTENSIONS, VALID_ID_PREFIX } from './constants'
88
import resolve from 'resolve'
99
import builtins from 'builtin-modules'
@@ -119,7 +119,14 @@ export function removeImportQuery(url: string) {
119119
}
120120

121121
export function injectQuery(url: string, queryToInject: string) {
122-
const { pathname, search, hash } = parseUrl(url)
122+
let resolvedUrl = new URL(url, 'relative:///')
123+
if (resolvedUrl.protocol !== 'relative:') {
124+
resolvedUrl = pathToFileURL(url)
125+
}
126+
let { protocol, pathname, search, hash } = resolvedUrl
127+
if (protocol === 'file:') {
128+
pathname = pathname.slice(1)
129+
}
123130
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
124131
hash || ''
125132
}`

0 commit comments

Comments
 (0)