Skip to content

Commit

Permalink
fix: splitFileAndPostfix works as cleanUrl (#12572)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
patak-dev and bluwy committed Mar 26, 2023
1 parent 4f9b8b4 commit 276725f
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit 276725f

Please sign in to comment.