Skip to content

Commit 34064c8

Browse files
committed
fix(resolve): handle hash fragment in fs resolve
1 parent 24ed098 commit 34064c8

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

packages/vite/src/node/plugins/resolve.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,24 @@ function tryFsResolve(
213213
options: InternalResolveOptions,
214214
tryIndex = true
215215
): string | undefined {
216-
const [file, q] = fsPath.split(`?`, 2)
217-
const query = q ? `?${q}` : ``
216+
let file = fsPath
217+
let postfix = ''
218+
219+
let postfixIndex = fsPath.indexOf('?')
220+
if (postfixIndex < 0) {
221+
postfixIndex = fsPath.indexOf('#')
222+
}
223+
if (postfixIndex > 0) {
224+
file = fsPath.slice(0, postfixIndex)
225+
postfix = fsPath.slice(postfixIndex)
226+
}
227+
218228
let res: string | undefined
219229
for (const ext of options.extensions || DEFAULT_EXTENSIONS) {
220230
if (
221231
(res = tryResolveFile(
222232
file + ext,
223-
query,
233+
postfix,
224234
options,
225235
false,
226236
options.tryPrefix
@@ -229,16 +239,17 @@ function tryFsResolve(
229239
return res
230240
}
231241
}
242+
232243
if (
233-
(res = tryResolveFile(file, query, options, tryIndex, options.tryPrefix))
244+
(res = tryResolveFile(file, postfix, options, tryIndex, options.tryPrefix))
234245
) {
235246
return res
236247
}
237248
}
238249

239250
function tryResolveFile(
240251
file: string,
241-
query: string,
252+
postfix: string,
242253
options: InternalResolveOptions,
243254
tryIndex: boolean,
244255
tryPrefix?: string
@@ -253,14 +264,14 @@ function tryResolveFile(
253264
return resolvePackageEntry(file, pkg, options)
254265
}
255266
const index = tryFsResolve(file + '/index', options)
256-
if (index) return index + query
267+
if (index) return index + postfix
257268
} else {
258-
return normalizePath(ensureVolumeInPath(file)) + query
269+
return normalizePath(ensureVolumeInPath(file)) + postfix
259270
}
260271
}
261272
if (tryPrefix) {
262273
const prefixed = `${path.dirname(file)}/${tryPrefix}${path.basename(file)}`
263-
return tryResolveFile(prefixed, query, options, tryIndex)
274+
return tryResolveFile(prefixed, postfix, options, tryIndex)
264275
}
265276
}
266277

0 commit comments

Comments
 (0)