Skip to content

Commit

Permalink
fix: when the file path is an absolute path, parsing causes parameter…
Browse files Browse the repository at this point in the history
… loss (#10449)
  • Loading branch information
yzydeveloper authored Oct 25, 2022
1 parent 9f268da commit df86990
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 12 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ describe('injectQuery', () => {
'C:/User/Vite/Project?direct'
)
})

test('absolute file path', () => {
expect(injectQuery('C:\\test-file.vue', 'direct')).toEqual(
'C:/test-file.vue?direct'
)
})

test('absolute file path with parameters', () => {
expect(
injectQuery('C:\\test-file.vue?vue&type=template&lang.js', 'direct')
).toEqual('C:/test-file.vue?direct&vue&type=template&lang.js')
})
}

test('relative path', () => {
Expand Down
7 changes: 2 additions & 5 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'node:os'
import path from 'node:path'
import { createHash } from 'node:crypto'
import { promisify } from 'node:util'
import { URL, URLSearchParams, pathToFileURL } from 'node:url'
import { URL, URLSearchParams } from 'node:url'
import { builtinModules, createRequire } from 'node:module'
import { promises as dns } from 'node:dns'
import { performance } from 'node:perf_hooks'
Expand Down Expand Up @@ -326,10 +326,7 @@ export function removeDirectQuery(url: string): string {
export function injectQuery(url: string, queryToInject: string): string {
// encode percents for consistent behavior with pathToFileURL
// see #2614 for details
let resolvedUrl = new URL(url.replace(/%/g, '%25'), 'relative:///')
if (resolvedUrl.protocol !== 'relative:') {
resolvedUrl = pathToFileURL(url)
}
const resolvedUrl = new URL(url.replace(/%/g, '%25'), 'relative:///')
const { search, hash } = resolvedUrl
let pathname = cleanUrl(url)
pathname = isWindows ? slash(pathname) : pathname
Expand Down

0 comments on commit df86990

Please sign in to comment.