Skip to content

Commit

Permalink
fix(css): remove empty css chunks with cssCodeSplit: false
Browse files Browse the repository at this point in the history
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 <opensource@fthiessen.de>
  • Loading branch information
susnux committed May 3, 2024
1 parent 2bc5d3d commit e0a0cf3
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}

if (chunkCSS) {
// for module output we need to remove pure css chunks
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,
Expand Down Expand Up @@ -838,6 +838,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}

// remove empty css chunks and their imports
let pureCssChunkNames: string[] = []
if (pureCssChunks.size) {
// map each pure css chunk (rendered chunk) to it's corresponding bundle
// chunk. we check that by `preliminaryFileName` as they have different
Expand All @@ -848,7 +849,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
.map((chunk) => [chunk.preliminaryFileName, chunk.fileName]),
)

const pureCssChunkNames = [...pureCssChunks].map(
pureCssChunkNames = [...pureCssChunks].map(
(pureCssChunk) => prelimaryNameToChunkMap[pureCssChunk.fileName],
)

Expand Down Expand Up @@ -885,13 +886,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
}
}

const removedPureCssFiles = removedPureCssFilesCache.get(config)!
pureCssChunkNames.forEach((fileName) => {
removedPureCssFiles.set(fileName, bundle[fileName] as RenderedChunk)
delete bundle[fileName]
delete bundle[`${fileName}.map`]
})
}

function extractCss() {
Expand Down Expand Up @@ -927,6 +921,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
source: extractedCss,
})
}

// remove pure CSS chunks only after the css has been extracted
const removedPureCssFiles = removedPureCssFilesCache.get(config)!
pureCssChunkNames.forEach((fileName) => {
removedPureCssFiles.set(fileName, bundle[fileName] as RenderedChunk)
delete bundle[fileName]
delete bundle[`${fileName}.map`]
})
},
}
}
Expand Down

0 comments on commit e0a0cf3

Please sign in to comment.