Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: splitFileAndPostfix works as cleanUrl #12572

Merged
merged 8 commits into from Mar 26, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 18 additions & 36 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -474,18 +474,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 @@ -495,32 +485,24 @@ 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.
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,
tryIndex,
targetWeb,
skipPackageJson,
)
if (res) return res + fsPath.slice(file.length)
}

const { file, postfix } = splitFileAndPostfix(fsPath)
const res = tryCleanFsResolve(
file,
options,
Expand Down