@@ -19,7 +19,13 @@ import type MagicString from 'magic-string'
19
19
20
20
import type { TransformResult } from 'rollup'
21
21
import { createFilter as _createFilter } from '@rollup/pluginutils'
22
- import { cleanUrl , isWindows , slash , withTrailingSlash } from '../shared/utils'
22
+ import {
23
+ cleanUrl ,
24
+ isWindows ,
25
+ slash ,
26
+ splitFileAndPostfix ,
27
+ withTrailingSlash ,
28
+ } from '../shared/utils'
23
29
import { VALID_ID_PREFIX } from '../shared/constants'
24
30
import {
25
31
CLIENT_ENTRY ,
@@ -311,20 +317,10 @@ export function removeRawQuery(url: string): string {
311
317
return url . replace ( rawRE , '$1' ) . replace ( trailingSeparatorRE , '' )
312
318
}
313
319
314
- const replacePercentageRE = / % / g
315
320
export function injectQuery ( url : string , queryToInject : string ) : string {
316
- // encode percents for consistent behavior with pathToFileURL
317
- // see #2614 for details
318
- const resolvedUrl = new URL (
319
- url . replace ( replacePercentageRE , '%25' ) ,
320
- 'relative:///' ,
321
- )
322
- const { search, hash } = resolvedUrl
323
- let pathname = cleanUrl ( url )
324
- pathname = isWindows ? slash ( pathname ) : pathname
325
- return `${ pathname } ?${ queryToInject } ${ search ? `&` + search . slice ( 1 ) : '' } ${
326
- hash ?? ''
327
- } `
321
+ const { file, postfix } = splitFileAndPostfix ( url )
322
+ const normalizedFile = isWindows ? slash ( file ) : file
323
+ return `${ normalizedFile } ?${ queryToInject } ${ postfix [ 0 ] === '?' ? `&${ postfix . slice ( 1 ) } ` : /* hash only */ postfix } `
328
324
}
329
325
330
326
const timestampRE = / \b t = \d { 13 } & ? \b /
0 commit comments