From bd52283a70a1451a4ad6f058787b18382d306880 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Mon, 4 Mar 2024 21:33:53 +0200 Subject: [PATCH] fix: analysing build chunk without dependencies (#15469) Co-authored-by: patak --- .../src/node/plugins/importAnalysisBuild.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 2311f0859ef4d3..8779ef80f78e20 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -5,7 +5,7 @@ import type { ImportSpecifier, } from 'es-module-lexer' import { init, parse as parseImports } from 'es-module-lexer' -import type { OutputChunk, SourceMap } from 'rollup' +import type { SourceMap } from 'rollup' import type { RawSourceMap } from '@ampproject/remapping' import convertSourceMap from 'convert-source-map' import { @@ -377,15 +377,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { if (filename === ownerFilename) return if (analyzed.has(filename)) return analyzed.add(filename) - const chunk = bundle[filename] as OutputChunk | undefined + const chunk = bundle[filename] if (chunk) { deps.add(chunk.fileName) - chunk.imports.forEach(addDeps) - // Ensure that the css imported by current chunk is loaded after the dependencies. - // So the style of current chunk won't be overwritten unexpectedly. - chunk.viteMetadata!.importedCss.forEach((file) => { - deps.add(file) - }) + if (chunk.type === 'chunk') { + chunk.imports.forEach(addDeps) + // Ensure that the css imported by current chunk is loaded after the dependencies. + // So the style of current chunk won't be overwritten unexpectedly. + chunk.viteMetadata!.importedCss.forEach((file) => { + deps.add(file) + }) + } } else { const removedPureCssFiles = removedPureCssFilesCache.get(config)!