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

refactor(css): use preliminaryFileName to detect pure CSS chunks #13974

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import {
arraify,
arrayEqual,
asyncReplace,
cleanUrl,
combineSourcemaps,
Expand Down Expand Up @@ -735,21 +734,17 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// remove empty css chunks and their imports
if (pureCssChunks.size) {
// map each pure css chunk (rendered chunk) to it's corresponding bundle
// chunk. we check that by comparing the `moduleIds` as they have different
// filenames (rendered chunk has the !~{XXX}~ placeholder)
const pureCssChunkNames: string[] = []
for (const pureCssChunk of pureCssChunks) {
for (const key in bundle) {
const bundleChunk = bundle[key]
if (
bundleChunk.type === 'chunk' &&
arrayEqual(bundleChunk.moduleIds, pureCssChunk.moduleIds)
) {
pureCssChunkNames.push(key)
break
}
}
}
// chunk. we check that by `preliminaryFileName` as they have different
// `filename`s (rendered chunk has the !~{XXX}~ placeholder)
const prelimaryNameToChunkMap = Object.fromEntries(
Object.values(bundle)
.filter((chunk): chunk is OutputChunk => chunk.type === 'chunk')
.map((chunk) => [chunk.preliminaryFileName, chunk.fileName]),
)

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

const emptyChunkFiles = pureCssChunkNames
.map((file) => path.basename(file))
Expand Down