@@ -30,6 +30,7 @@ import { CLIENT_PUBLIC_PATH, SPECIAL_QUERY_RE } from '../constants'
30
30
import type { ResolvedConfig } from '../config'
31
31
import type { Plugin } from '../plugin'
32
32
import {
33
+ arrayEqual ,
33
34
asyncReplace ,
34
35
cleanUrl ,
35
36
combineSourcemaps ,
@@ -305,7 +306,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
305
306
export function cssPostPlugin ( config : ResolvedConfig ) : Plugin {
306
307
// styles initialization in buildStart causes a styling loss in watch
307
308
const styles : Map < string , string > = new Map < string , string > ( )
308
- let pureCssChunks : Set < string >
309
+ let pureCssChunks : Set < RenderedChunk >
309
310
310
311
// when there are multiple rollup outputs and extracting CSS, only emit once,
311
312
// since output formats have no effect on the generated CSS.
@@ -339,7 +340,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
339
340
340
341
buildStart ( ) {
341
342
// Ensure new caches for every build (i.e. rebuilding in watch mode)
342
- pureCssChunks = new Set < string > ( )
343
+ pureCssChunks = new Set < RenderedChunk > ( )
343
344
outputToExtractedCSSMap = new Map < NormalizedOutputOptions , string > ( )
344
345
hasEmitted = false
345
346
} ,
@@ -537,7 +538,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
537
538
if ( config . build . cssCodeSplit ) {
538
539
if ( isPureCssChunk ) {
539
540
// this is a shared CSS-only chunk that is empty.
540
- pureCssChunks . add ( chunk . fileName )
541
+ pureCssChunks . add ( chunk )
541
542
}
542
543
if ( opts . format === 'es' || opts . format === 'cjs' ) {
543
544
const cssAssetName = chunk . facadeModuleId
@@ -624,7 +625,24 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
624
625
625
626
// remove empty css chunks and their imports
626
627
if ( pureCssChunks . size ) {
627
- const emptyChunkFiles = [ ...pureCssChunks ]
628
+ // map each pure css chunk (rendered chunk) to it's corresponding bundle
629
+ // chunk. we check that by comparing the `moduleIds` as they have different
630
+ // filenames (rendered chunk has the !~{XXX}~ placeholder)
631
+ const pureCssChunkNames : string [ ] = [ ]
632
+ for ( const pureCssChunk of pureCssChunks ) {
633
+ for ( const key in bundle ) {
634
+ const bundleChunk = bundle [ key ]
635
+ if (
636
+ bundleChunk . type === 'chunk' &&
637
+ arrayEqual ( bundleChunk . moduleIds , pureCssChunk . moduleIds )
638
+ ) {
639
+ pureCssChunkNames . push ( key )
640
+ break
641
+ }
642
+ }
643
+ }
644
+
645
+ const emptyChunkFiles = pureCssChunkNames
628
646
. map ( ( file ) => path . basename ( file ) )
629
647
. join ( '|' )
630
648
. replace ( / \. / g, '\\.' )
@@ -641,7 +659,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
641
659
// and also register the emitted CSS files under the importer
642
660
// chunks instead.
643
661
chunk . imports = chunk . imports . filter ( ( file ) => {
644
- if ( pureCssChunks . has ( file ) ) {
662
+ if ( pureCssChunkNames . includes ( file ) ) {
645
663
const {
646
664
viteMetadata : { importedCss }
647
665
} = bundle [ file ] as OutputChunk
@@ -660,7 +678,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
660
678
}
661
679
}
662
680
const removedPureCssFiles = removedPureCssFilesCache . get ( config ) !
663
- pureCssChunks . forEach ( ( fileName ) => {
681
+ pureCssChunkNames . forEach ( ( fileName ) => {
664
682
removedPureCssFiles . set ( fileName , bundle [ fileName ] as RenderedChunk )
665
683
delete bundle [ fileName ]
666
684
} )
0 commit comments