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)!