diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 3b7135a1af1c74..e5721bef0f2d49 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -300,11 +300,9 @@ function esbuildScanPlugin( '@vite/env', ] - const isUnlessEntry = (path: string) => !entries.includes(path) - const externalUnlessEntry = ({ path }: { path: string }) => ({ path, - external: isUnlessEntry(path), + external: !entries.includes(path), }) const doTransformGlobImport = async ( @@ -551,39 +549,25 @@ function esbuildScanPlugin( // they are done after the bare import resolve because a package name // may end with these extensions - const setupExternalize = ( - filter: RegExp, - doExternalize: (path: string) => boolean, - ) => { - build.onResolve({ filter }, ({ path }) => { - return { - path, - external: doExternalize(path), - } - }) - // onResolve is not called for glob imports. - // we need to add that here as well until esbuild calls onResolve for glob imports. - // https://github.com/evanw/esbuild/issues/3317 - build.onLoad({ filter, namespace: 'file' }, () => { - const externalOnLoadResult: OnLoadResult = { - loader: 'js', - contents: 'export default {}', - } - return externalOnLoadResult - }) - } - // css - setupExternalize(CSS_LANGS_RE, isUnlessEntry) + build.onResolve({ filter: CSS_LANGS_RE }, externalUnlessEntry) + // json & wasm - setupExternalize(/\.(json|json5|wasm)$/, isUnlessEntry) + build.onResolve({ filter: /\.(json|json5|wasm)$/ }, externalUnlessEntry) + // known asset types - setupExternalize( - new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`), - isUnlessEntry, + build.onResolve( + { + filter: new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`), + }, + externalUnlessEntry, ) + // known vite query types: ?worker, ?raw - setupExternalize(SPECIAL_QUERY_RE, () => true) + build.onResolve({ filter: SPECIAL_QUERY_RE }, ({ path }) => ({ + path, + external: true, + })) // catch all -------------------------------------------------------------