From 1fe79dd4b05cbbce6ec433df0067006439cd8384 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 24 Mar 2023 23:46:10 +0100 Subject: [PATCH 1/6] refactor: cleanUrl works as splitFileAndPostfix --- packages/vite/src/node/utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 4eb5bef7ae87f3..36e3899f4a08ec 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -258,8 +258,11 @@ export function ensureVolumeInPath(file: string): string { export const queryRE = /\?.*$/s export const hashRE = /#.*$/s -export const cleanUrl = (url: string): string => - url.replace(hashRE, '').replace(queryRE, '') +// remove query and hash, a # before ? is valid +export function cleanUrl(url: string): string { + const u = url.replace(queryRE, '') + return u === url ? u.replace(hashRE, '') : u +} export const externalRE = /^(https?:)?\/\// export const isExternalUrl = (url: string): boolean => externalRE.test(url) From 90bec711ef8e35a895e264e7b3162a4d3ec071e2 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 25 Mar 2023 00:35:45 +0100 Subject: [PATCH 2/6] chore: update --- packages/vite/src/node/plugins/resolve.ts | 53 ++++++++--------------- packages/vite/src/node/utils.ts | 6 +-- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 581b1a3368c4c8..6a91b92e4425b4 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -35,6 +35,7 @@ import { isWindows, lookupFile, normalizePath, + queryRE, resolveFrom, safeRealpathSync, slash, @@ -474,18 +475,8 @@ function ensureVersionQuery( } function splitFileAndPostfix(path: string) { - let file = path - let postfix = '' - - let postfixIndex = path.indexOf('?') - if (postfixIndex < 0) { - postfixIndex = path.indexOf('#') - } - if (postfixIndex > 0) { - file = path.slice(0, postfixIndex) - postfix = path.slice(postfixIndex) - } - return { file, postfix } + const file = cleanUrl(path) + return { file, postfix: path.slice(file.length) } } function tryFsResolve( @@ -495,32 +486,22 @@ function tryFsResolve( targetWeb = true, skipPackageJson = false, ): string | undefined { - let postfixIndex = fsPath.indexOf('?') - if (postfixIndex < 0) { - postfixIndex = fsPath.indexOf('#') - - // Dependencies like es5-ext use `#` in their paths. We don't support `#` in user - // source code so we only need to perform the check for dependencies. - // We don't support `?` in node_modules paths, so we only need to check in this branch. - if (postfixIndex >= 0 && fsPath.includes('node_modules')) { - const res = tryCleanFsResolve( - fsPath, - options, - tryIndex, - targetWeb, - skipPackageJson, - ) - if (res) return res - } - } - - let file = fsPath - let postfix = '' - if (postfixIndex >= 0) { - file = fsPath.slice(0, postfixIndex) - postfix = fsPath.slice(postfixIndex) + // Dependencies like es5-ext use `#` in their paths. We don't support `#` in user + // source code so we only need to perform the check for dependencies. + // We don't support `?` in node_modules paths, so we only need to check in this branch. + if (fsPath.includes('#') && fsPath.includes('node_modules')) { + const file = fsPath.replace(queryRE, '') + const res = tryCleanFsResolve( + file, + options, + tryIndex, + targetWeb, + skipPackageJson, + ) + if (res) return res + fsPath.slice(file.length) } + const { file, postfix } = splitFileAndPostfix(fsPath) const res = tryCleanFsResolve( file, options, diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 36e3899f4a08ec..81ebda7b98e7db 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -256,12 +256,10 @@ export function ensureVolumeInPath(file: string): string { } export const queryRE = /\?.*$/s -export const hashRE = /#.*$/s -// remove query and hash, a # before ? is valid +const postfixRE = /[?#].*$/s export function cleanUrl(url: string): string { - const u = url.replace(queryRE, '') - return u === url ? u.replace(hashRE, '') : u + return url.replace(postfixRE, '') } export const externalRE = /^(https?:)?\/\// From 28c5dc7d7ec92d70b06568161a9c5730d9c66ac7 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 25 Mar 2023 00:54:00 +0100 Subject: [PATCH 3/6] chore: update --- packages/vite/src/node/plugins/resolve.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 6a91b92e4425b4..af33368f361608 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -489,8 +489,10 @@ function tryFsResolve( // Dependencies like es5-ext use `#` in their paths. We don't support `#` in user // source code so we only need to perform the check for dependencies. // We don't support `?` in node_modules paths, so we only need to check in this branch. - if (fsPath.includes('#') && fsPath.includes('node_modules')) { - const file = fsPath.replace(queryRE, '') + const hashIndex = fsPath.indexOf('#') + if (hashIndex > 0 && fsPath.includes('node_modules')) { + const queryIndex = fsPath.indexOf('?') + const file = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath const res = tryCleanFsResolve( file, options, From d64a151502f9b14b13fd8d2a623e321a55caf008 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 25 Mar 2023 01:00:04 +0100 Subject: [PATCH 4/6] chore: lint --- packages/vite/src/node/plugins/resolve.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index af33368f361608..fbde55c94e59df 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -35,7 +35,6 @@ import { isWindows, lookupFile, normalizePath, - queryRE, resolveFrom, safeRealpathSync, slash, From 863bd81d0fb2467126770e050f387c46fd376a58 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 26 Mar 2023 20:36:21 +0200 Subject: [PATCH 5/6] chore: ignore foo?bar#baz --- packages/vite/src/node/plugins/resolve.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 3cd94801f324b3..fedef26b081855 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -484,15 +484,18 @@ function tryFsResolve( const hashIndex = fsPath.indexOf('#') if (hashIndex > 0 && isInNodeModules(fsPath)) { const queryIndex = fsPath.indexOf('?') - const file = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath - const res = tryCleanFsResolve( - file, - options, - tryIndex, - targetWeb, - skipPackageJson, - ) - if (res) return res + fsPath.slice(file.length) + // We only need to check foo#bar?baz and foo#bar, ignore foo?bar#baz + if (queryIndex < 0 || queryIndex > hashIndex) { + const file = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath + const res = tryCleanFsResolve( + file, + options, + tryIndex, + targetWeb, + skipPackageJson, + ) + if (res) return res + fsPath.slice(file.length) + } } const { file, postfix } = splitFileAndPostfix(fsPath) From 980e09b00b2db8db7dd96339b3a3cfb5cc987f69 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 26 Mar 2023 21:10:56 +0200 Subject: [PATCH 6/6] fix: update Co-authored-by: Bjorn Lu --- packages/vite/src/node/plugins/resolve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index fedef26b081855..0d6b732e5ade2c 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -482,7 +482,7 @@ function tryFsResolve( // source code so we only need to perform the check for dependencies. // We don't support `?` in node_modules paths, so we only need to check in this branch. const hashIndex = fsPath.indexOf('#') - if (hashIndex > 0 && isInNodeModules(fsPath)) { + if (hashIndex >= 0 && isInNodeModules(fsPath)) { const queryIndex = fsPath.indexOf('?') // We only need to check foo#bar?baz and foo#bar, ignore foo?bar#baz if (queryIndex < 0 || queryIndex > hashIndex) {