diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 03ead5b564a56e..47bbac7ce5fec6 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -622,14 +622,20 @@ export function runOptimizeDeps( return context .build() .then((result) => { + const depsForSrc: Record = {} + for (const dep of Object.values(depsInfo)) { + if (dep.src) { + // One chunk maybe corresponding multiply entry + depsForSrc[dep.src] ||= [] + depsForSrc[dep.src].push(dep) + } + } + for (const chunk of result.output) { if (chunk.type !== 'chunk') continue if (chunk.isEntry) { - // One chunk maybe corresponding multiply entry - const deps = Object.values(depsInfo).filter( - (d) => d.src === normalizePath(chunk.facadeModuleId!), - ) + const deps = depsForSrc[normalizePath(chunk.facadeModuleId!)] for (const { exportsData, file, id, ...info } of deps) { addOptimizedDepInfo(metadata, 'optimized', { id, diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 93edba4b25c92d..a4356b509795a8 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -293,7 +293,7 @@ async function prepareRolldownScanner( const plugins = await asyncFlatten(arraify(pluginsFromConfig)) - plugins.push(rolldownScanPlugin(environment, deps, missing, entries)) + plugins.push(...rolldownScanPlugin(environment, deps, missing, entries)) async function build() { await scan({ @@ -350,7 +350,7 @@ function rolldownScanPlugin( depImports: Record, missing: Record, entries: string[], -): Plugin { +): Plugin[] { const seen = new Map() async function resolveId( id: string, @@ -533,182 +533,203 @@ function rolldownScanPlugin( const ASSET_TYPE_RE = new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`) - return { - name: 'vite:dep-scan', - async resolveId(id, importer) { - // external urls - if (externalRE.test(id)) { - return { - id, - external: true, + return [ + { + name: 'vite:dep-scan:resolve', + async resolveId(id, importer) { + // external urls + if (externalRE.test(id)) { + return { + id, + external: true, + } } - } - // data urls - if (dataUrlRE.test(id)) { - return { - id, - external: true, + // data urls + if (dataUrlRE.test(id)) { + return { + id, + external: true, + } } - } - - // local scripts (`