From 276725f9c3d166a23efc297cbedc3b59d8cdb5e8 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 26 Mar 2023 21:20:23 +0200 Subject: [PATCH] fix: splitFileAndPostfix works as cleanUrl (#12572) Co-authored-by: Bjorn Lu --- packages/vite/src/node/plugins/resolve.ts | 43 ++++++++--------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 0013f36e7b36cc..0d6b732e5ade2c 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -467,18 +467,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( @@ -488,32 +478,27 @@ 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 && isInNodeModules(fsPath)) { + // 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. + const hashIndex = fsPath.indexOf('#') + 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) { + const file = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath const res = tryCleanFsResolve( - fsPath, + file, options, tryIndex, targetWeb, skipPackageJson, ) - if (res) return res + if (res) return res + fsPath.slice(file.length) } } - let file = fsPath - let postfix = '' - if (postfixIndex >= 0) { - file = fsPath.slice(0, postfixIndex) - postfix = fsPath.slice(postfixIndex) - } - + const { file, postfix } = splitFileAndPostfix(fsPath) const res = tryCleanFsResolve( file, options,