From 456e3cdc4cc46eae22419bab29315523a6bedd9c Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 3 May 2024 11:45:59 +0200 Subject: [PATCH] fix(css): remove empty css chunks with `cssCodeSplit: false` Resolves #16582 When setting `cssCodeSplit: false` the styles are still split into one style file. But currently the imported CSS results in empty chunks, as in the current implementation empty chunks are only removed when `cssCodeSplit` was set to `true`. So this also adds empty style chunks to `pureCSSChunks` even with `cssCodeSplit: false`. Signed-off-by: Ferdinand Thiessen --- packages/vite/src/node/plugins/css.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 0e97c247cf01f8..9cfe8646caf172 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -723,13 +723,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { } if (chunkCSS) { + if (isPureCssChunk && (opts.format === 'es' || opts.format === 'cjs')) { + // this is a shared CSS-only chunk that is empty. + pureCssChunks.add(chunk) + } if (config.build.cssCodeSplit) { if (opts.format === 'es' || opts.format === 'cjs') { - if (isPureCssChunk) { - // this is a shared CSS-only chunk that is empty. - pureCssChunks.add(chunk) - } - const isEntry = chunk.isEntry && isPureCssChunk const cssFullAssetName = ensureFileExt(chunk.name, '.css') // if facadeModuleId doesn't exist or doesn't have a CSS extension,