diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 96e0890de3519b..0e97c247cf01f8 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -860,6 +860,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { for (const file in bundle) { const chunk = bundle[file] if (chunk.type === 'chunk') { + let chunkImportsPureCssChunk = false // remove pure css chunk from other chunk's imports, // and also register the emitted CSS files under the importer // chunks instead. @@ -874,11 +875,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { importedAssets.forEach((file) => chunk.viteMetadata!.importedAssets.add(file), ) + chunkImportsPureCssChunk = true return false } return true }) - chunk.code = replaceEmptyChunk(chunk.code) + if (chunkImportsPureCssChunk) { + chunk.code = replaceEmptyChunk(chunk.code) + } } }