From fa4d5b81092506679aa2f2a8307cf9f522cc18b0 Mon Sep 17 00:00:00 2001 From: yoho Date: Fri, 19 Aug 2022 22:19:14 +0800 Subject: [PATCH 1/9] fix: injectQuery --- packages/vite/src/node/__tests__/utils.spec.ts | 18 ++++++++++++++++++ packages/vite/src/node/utils.ts | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 51990edf709da2..34f0363a9dc741 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -19,6 +19,24 @@ 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' + ) + }) + + test('file path', () => { + expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch( + new RegExp(`${process.cwd().slice(1)}/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' diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 85f85adf3cf550..1ef6d8a204b61d 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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) From 574b03576f737b7d56138434d60a1e40e03da1f6 Mon Sep 17 00:00:00 2001 From: yoho Date: Fri, 19 Aug 2022 22:32:19 +0800 Subject: [PATCH 2/9] fix: test --- packages/vite/src/node/__tests__/utils.spec.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 34f0363a9dc741..d5804fc17d8a6d 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -33,7 +33,13 @@ describe('injectQuery', () => { test('file path', () => { expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch( - new RegExp(`${process.cwd().slice(1)}/file:/usr/vite/%20a%20\\?direct`) + 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(`${process.cwd()}/file:/usr/vite/%20a%20\\?direct`) ) }) From fdcf83bb54c7e8aef7b7d1bcbef21d0e26d1df0b Mon Sep 17 00:00:00 2001 From: yoho Date: Fri, 19 Aug 2022 22:36:09 +0800 Subject: [PATCH 3/9] fix: test --- packages/vite/src/node/__tests__/utils.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index d5804fc17d8a6d..4ebd482db16889 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -33,7 +33,7 @@ describe('injectQuery', () => { test('file path', () => { expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch( - isWindows + !isWindows ? // d/a/vite/vite/file:/usr/vite/%20a%20?direct new RegExp( `${process.cwd().slice(1)}/file:/usr/vite/%20a%20\\?direct` From 29fa12a725425859133b4fd0862fa8cea7a7fbcb Mon Sep 17 00:00:00 2001 From: yoho Date: Fri, 19 Aug 2022 22:56:55 +0800 Subject: [PATCH 4/9] fix: test --- packages/vite/src/node/__tests__/utils.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 4ebd482db16889..0519567b055b80 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -6,6 +6,7 @@ import { getPotentialTsSrcPaths, injectQuery, isWindows, + normalizePath, resolveHostname } from '../utils' @@ -39,7 +40,9 @@ describe('injectQuery', () => { `${process.cwd().slice(1)}/file:/usr/vite/%20a%20\\?direct` ) : // D:/a/vite/vite/file:/usr/vite/%20a%20?direct - new RegExp(`${process.cwd()}/file:/usr/vite/%20a%20\\?direct`) + new RegExp( + `${normalizePath(process.cwd())}/file:/usr/vite/%20a%20\\?direct` + ) ) }) From e5298bf916475f85fed76cf75a4eb82815984829 Mon Sep 17 00:00:00 2001 From: yoho Date: Sat, 20 Aug 2022 17:27:20 +0800 Subject: [PATCH 5/9] fix: pathname --- packages/vite/src/node/__tests__/utils.spec.ts | 18 ++++++++---------- packages/vite/src/node/utils.ts | 14 ++++---------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 0519567b055b80..7d5fa7270351dd 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -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`) ) }) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 1ef6d8a204b61d..aa12565b377eac 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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/ From 58ac4235bf8feee6410b540ef2d52c10d7652e65 Mon Sep 17 00:00:00 2001 From: yoho Date: Sat, 20 Aug 2022 17:39:56 +0800 Subject: [PATCH 6/9] fix: test --- packages/vite/src/node/__tests__/utils.spec.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 7d5fa7270351dd..3e3bda25895b36 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -15,32 +15,29 @@ describe('injectQuery', () => { // this test will work incorrectly on unix systems test('normalize windows path', () => { expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual( - 'C:/User/Vite/Project?direct' + 'C:\\User\\Vite\\Project?direct' ) }) } - test('relative path ""', () => { + 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' ) - }) - - test('relative path "../"', () => { expect(injectQuery('../usr/vite/%20a%20', 'direct')).toEqual( '../usr/vite/%20a%20?direct' ) }) - test('file path', () => { + test('path with protocol', () => { expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch( - new RegExp(`file:///usr/vite/%20a%20\\?direct`) + 'file:///usr/vite/%20a%20?direct' + ) + expect(injectQuery('http://usr.vite/%20a%20', 'direct')).toMatch( + 'http://usr.vite/%20a%20?direct' ) }) From bcaea1ab63dbb4547a69da3afe6a8fd048037c05 Mon Sep 17 00:00:00 2001 From: yoho Date: Sat, 20 Aug 2022 23:15:29 +0800 Subject: [PATCH 7/9] chore: update --- packages/vite/src/node/__tests__/utils.spec.ts | 9 +++++++-- packages/vite/src/node/utils.ts | 8 +++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 3e3bda25895b36..858ec8437362a1 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -6,7 +6,6 @@ import { getPotentialTsSrcPaths, injectQuery, isWindows, - normalizePath, resolveHostname } from '../utils' @@ -15,7 +14,7 @@ describe('injectQuery', () => { // this test will work incorrectly on unix systems test('normalize windows path', () => { expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual( - 'C:\\User\\Vite\\Project?direct' + 'C:/User/Vite/Project?direct' ) }) } @@ -32,6 +31,12 @@ describe('injectQuery', () => { ) }) + test('path with hash', () => { + expect(injectQuery('/usr/vite/path with space/#1?2/', 'direct')).toEqual( + '/usr/vite/path with space/?direct#1?2/' + ) + }) + test('path with protocol', () => { expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch( 'file:///usr/vite/%20a%20?direct' diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index aa12565b377eac..bd48882823f857 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -312,9 +312,11 @@ export function injectQuery(url: string, queryToInject: string): string { resolvedUrl = pathToFileURL(url) } const { search, hash } = resolvedUrl - return `${url.split('?')[0]}?${queryToInject}${ - search ? `&` + search.slice(1) : '' - }${hash ?? ''}` + let pathname = normalizePath(url.replace(/#.*$/, '').replace(/\?.*$/, '')) + pathname = isWindows ? slash(pathname) : pathname + return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${ + hash ?? '' + }` } const timestampRE = /\bt=\d{13}&?\b/ From b1b6cc81d3e1b9b2928c5cbc6ca56b11ba974c81 Mon Sep 17 00:00:00 2001 From: yoho Date: Sat, 20 Aug 2022 23:20:01 +0800 Subject: [PATCH 8/9] chore: update --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index bd48882823f857..a9e37d162e491b 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -312,7 +312,7 @@ export function injectQuery(url: string, queryToInject: string): string { resolvedUrl = pathToFileURL(url) } const { search, hash } = resolvedUrl - let pathname = normalizePath(url.replace(/#.*$/, '').replace(/\?.*$/, '')) + let pathname = normalizePath(cleanUrl(url)) pathname = isWindows ? slash(pathname) : pathname return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${ hash ?? '' From 737c54df35af22bb5c7328ed96f9a235d49aa642 Mon Sep 17 00:00:00 2001 From: yoho Date: Sun, 21 Aug 2022 10:46:02 +0800 Subject: [PATCH 9/9] chore: update --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index a9e37d162e491b..656bbb22279069 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -312,7 +312,7 @@ export function injectQuery(url: string, queryToInject: string): string { resolvedUrl = pathToFileURL(url) } const { search, hash } = resolvedUrl - let pathname = normalizePath(cleanUrl(url)) + let pathname = cleanUrl(url) pathname = isWindows ? slash(pathname) : pathname return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${ hash ?? ''