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: injectQuery break relative path #9760

Merged
merged 9 commits into from Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Expand Up @@ -6,6 +6,7 @@ import {
getPotentialTsSrcPaths,
injectQuery,
isWindows,
normalizePath,
resolveHostname
} from '../utils'

Expand All @@ -19,6 +20,32 @@ describe('injectQuery', () => {
})
}

test('relative path ""', () => {
expect(injectQuery('usr/vite/%20a%20', 'direct')).toEqual(
'usr/vite/%20a%20?direct'
)
})

test('relative path "./"', () => {
expect(injectQuery('./usr/vite/%20a%20', 'direct')).toEqual(
'usr/vite/%20a%20?direct'
)
})
poyoho marked this conversation as resolved.
Show resolved Hide resolved

test('file path', () => {
expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch(
!isWindows
? // d/a/vite/vite/file:/usr/vite/%20a%20?direct
new RegExp(
`${process.cwd().slice(1)}/file:/usr/vite/%20a%20\\?direct`
)
: // D:/a/vite/vite/file:/usr/vite/%20a%20?direct
poyoho marked this conversation as resolved.
Show resolved Hide resolved
new RegExp(
`${normalizePath(process.cwd())}/file:/usr/vite/%20a%20\\?direct`
)
)
})

test('path with multiple spaces', () => {
expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual(
'/usr/vite/path with space?direct'
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -312,7 +312,9 @@ export function injectQuery(url: string, queryToInject: string): string {
resolvedUrl = pathToFileURL(url)
}
let { protocol, pathname, search, hash } = resolvedUrl
if (protocol === 'file:') {
// pathname must startWith '/'
// if url[0] is not '/' should be relative path
if (protocol === 'file:' || url[0] !== '/') {
pathname = pathname.slice(1)
}
pathname = decodeURIComponent(pathname)
Expand Down