Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): also remove empty style chunk when cssCodeSplit: false (fixes #16582) #16585

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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