Skip to content

Commit

Permalink
fix(resolve): handle hash fragment in fs resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 16, 2021
1 parent 24ed098 commit 34064c8
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,24 @@ function tryFsResolve(
options: InternalResolveOptions,
tryIndex = true
): string | undefined {
const [file, q] = fsPath.split(`?`, 2)
const query = q ? `?${q}` : ``
let file = fsPath
let postfix = ''

let postfixIndex = fsPath.indexOf('?')
if (postfixIndex < 0) {
postfixIndex = fsPath.indexOf('#')
}
if (postfixIndex > 0) {
file = fsPath.slice(0, postfixIndex)
postfix = fsPath.slice(postfixIndex)
}

let res: string | undefined
for (const ext of options.extensions || DEFAULT_EXTENSIONS) {
if (
(res = tryResolveFile(
file + ext,
query,
postfix,
options,
false,
options.tryPrefix
Expand All @@ -229,16 +239,17 @@ function tryFsResolve(
return res
}
}

if (
(res = tryResolveFile(file, query, options, tryIndex, options.tryPrefix))
(res = tryResolveFile(file, postfix, options, tryIndex, options.tryPrefix))
) {
return res
}
}

function tryResolveFile(
file: string,
query: string,
postfix: string,
options: InternalResolveOptions,
tryIndex: boolean,
tryPrefix?: string
Expand All @@ -253,14 +264,14 @@ function tryResolveFile(
return resolvePackageEntry(file, pkg, options)
}
const index = tryFsResolve(file + '/index', options)
if (index) return index + query
if (index) return index + postfix
} else {
return normalizePath(ensureVolumeInPath(file)) + query
return normalizePath(ensureVolumeInPath(file)) + postfix
}
}
if (tryPrefix) {
const prefixed = `${path.dirname(file)}/${tryPrefix}${path.basename(file)}`
return tryResolveFile(prefixed, query, options, tryIndex)
return tryResolveFile(prefixed, postfix, options, tryIndex)
}
}

Expand Down

0 comments on commit 34064c8

Please sign in to comment.