Skip to content

Commit

Permalink
refactor(css): use preliminaryFileName to detect pure CSS chunks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Aug 10, 2023
1 parent dcee6ef commit 835249d
Showing 1 changed file with 11 additions and 16 deletions.
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

0 comments on commit 835249d

Please sign in to comment.