Skip to content

Commit

Permalink
fix: pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Aug 20, 2022
1 parent 29fa12a commit e5298bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
18 changes: 8 additions & 10 deletions packages/vite/src/node/__tests__/utils.spec.ts
Expand Up @@ -28,21 +28,19 @@ describe('injectQuery', () => {

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

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

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
new RegExp(
`${normalizePath(process.cwd())}/file:/usr/vite/%20a%20\\?direct`
)
new RegExp(`file:///usr/vite/%20a%20\\?direct`)
)
})

Expand Down
14 changes: 4 additions & 10 deletions packages/vite/src/node/utils.ts
Expand Up @@ -311,16 +311,10 @@ export function injectQuery(url: string, queryToInject: string): string {
if (resolvedUrl.protocol !== 'relative:') {
resolvedUrl = pathToFileURL(url)
}
let { protocol, pathname, search, hash } = resolvedUrl
// pathname must startWith '/'
// if url[0] is not '/' should be relative path
if (protocol === 'file:' || url[0] !== '/') {
pathname = pathname.slice(1)
}
pathname = decodeURIComponent(pathname)
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash ?? ''
}`
const { search, hash } = resolvedUrl
return `${url.split('?')[0]}?${queryToInject}${
search ? `&` + search.slice(1) : ''
}${hash ?? ''}`
}

const timestampRE = /\bt=\d{13}&?\b/
Expand Down

0 comments on commit e5298bf

Please sign in to comment.