From e2658ad6fe81278069d75d0b3b9c260c3021b922 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 5 Apr 2024 10:30:20 +0800 Subject: [PATCH] perf(css): only replace empty chunk if imported (#16349) --- packages/vite/src/node/plugins/css.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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) + } } }