Skip to content

Commit 676804d

Browse files
XiSenaosapphi-red
andauthored
fix: esbuild glob import resolve error (#15140)
Co-authored-by: sapphi-red <[email protected]>
1 parent 0571b7c commit 676804d

File tree

1 file changed

+31
-15
lines changed
  • packages/vite/src/node/optimizer

1 file changed

+31
-15
lines changed

packages/vite/src/node/optimizer/scan.ts

+31-15
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,11 @@ function esbuildScanPlugin(
300300
'@vite/env',
301301
]
302302

303+
const isUnlessEntry = (path: string) => !entries.includes(path)
304+
303305
const externalUnlessEntry = ({ path }: { path: string }) => ({
304306
path,
305-
external: !entries.includes(path),
307+
external: isUnlessEntry(path),
306308
})
307309

308310
const doTransformGlobImport = async (
@@ -549,25 +551,39 @@ function esbuildScanPlugin(
549551
// they are done after the bare import resolve because a package name
550552
// may end with these extensions
551553

552-
// css
553-
build.onResolve({ filter: CSS_LANGS_RE }, externalUnlessEntry)
554+
const setupExternalize = (
555+
filter: RegExp,
556+
doExternalize: (path: string) => boolean,
557+
) => {
558+
build.onResolve({ filter }, ({ path }) => {
559+
return {
560+
path,
561+
external: doExternalize(path),
562+
}
563+
})
564+
// onResolve is not called for glob imports.
565+
// we need to add that here as well until esbuild calls onResolve for glob imports.
566+
// https://github.com/evanw/esbuild/issues/3317
567+
build.onLoad({ filter, namespace: 'file' }, () => {
568+
const externalOnLoadResult: OnLoadResult = {
569+
loader: 'js',
570+
contents: 'export default {}',
571+
}
572+
return externalOnLoadResult
573+
})
574+
}
554575

576+
// css
577+
setupExternalize(CSS_LANGS_RE, isUnlessEntry)
555578
// json & wasm
556-
build.onResolve({ filter: /\.(json|json5|wasm)$/ }, externalUnlessEntry)
557-
579+
setupExternalize(/\.(json|json5|wasm)$/, isUnlessEntry)
558580
// known asset types
559-
build.onResolve(
560-
{
561-
filter: new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`),
562-
},
563-
externalUnlessEntry,
581+
setupExternalize(
582+
new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`),
583+
isUnlessEntry,
564584
)
565-
566585
// known vite query types: ?worker, ?raw
567-
build.onResolve({ filter: SPECIAL_QUERY_RE }, ({ path }) => ({
568-
path,
569-
external: true,
570-
}))
586+
setupExternalize(SPECIAL_QUERY_RE, () => true)
571587

572588
// catch all -------------------------------------------------------------
573589

0 commit comments

Comments
 (0)