Skip to content

Commit

Permalink
chore: integrate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
XiSenao committed Dec 9, 2023
1 parent e6d72fc commit 930d290
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,11 @@ function esbuildScanPlugin(
'@vite/env',
]

const isUnlessEntry = (path: string) => !entries.includes(path)

const externalUnlessEntry = ({ path }: { path: string }) => ({
path,
external: !entries.includes(path),
external: isUnlessEntry(path),
})

const doTransformGlobImport = async (
Expand Down Expand Up @@ -549,25 +551,29 @@ function esbuildScanPlugin(
// they are done after the bare import resolve because a package name
// may end with these extensions

// css
build.onResolve({ filter: CSS_LANGS_RE }, externalUnlessEntry)
const setupExternalize = (
filter: RegExp,
doExternalize: (path: string) => boolean,
) => {
build.onResolve({ filter }, ({ path }) => {
return {
path,
external: doExternalize(path),
}
})
}

// css
setupExternalize(CSS_LANGS_RE, isUnlessEntry)
// json & wasm
build.onResolve({ filter: /\.(json|json5|wasm)$/ }, externalUnlessEntry)

setupExternalize(/\.(json|json5|wasm)$/, isUnlessEntry)
// known asset types
build.onResolve(
{
filter: new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`),
},
externalUnlessEntry,
setupExternalize(
new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`),
isUnlessEntry,
)

// known vite query types: ?worker, ?raw
build.onResolve({ filter: SPECIAL_QUERY_RE }, ({ path }) => ({
path,
external: true,
}))
setupExternalize(SPECIAL_QUERY_RE, () => true)

// catch all -------------------------------------------------------------

Expand Down

0 comments on commit 930d290

Please sign in to comment.