Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when the file path is an absolute path, parsing causes parameter loss #10449

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -322,10 +322,7 @@ export function removeImportQuery(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