@@ -6,7 +6,6 @@ import glob from 'fast-glob'
6
6
import postcssrc from 'postcss-load-config'
7
7
import type {
8
8
ExistingRawSourceMap ,
9
- NormalizedOutputOptions ,
10
9
OutputChunk ,
11
10
RenderedChunk ,
12
11
RollupError ,
@@ -376,8 +375,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
376
375
377
376
// when there are multiple rollup outputs and extracting CSS, only emit once,
378
377
// since output formats have no effect on the generated CSS.
379
- let outputToExtractedCSSMap : Map < NormalizedOutputOptions , string >
380
378
let hasEmitted = false
379
+ let chunkCSSMap : Map < string , string >
381
380
382
381
const rollupOptionsOutput = config . build . rollupOptions . output
383
382
const assetFileNames = (
@@ -407,8 +406,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
407
406
renderStart ( ) {
408
407
// Ensure new caches for every build (i.e. rebuilding in watch mode)
409
408
pureCssChunks = new Set < RenderedChunk > ( )
410
- outputToExtractedCSSMap = new Map < NormalizedOutputOptions , string > ( )
411
409
hasEmitted = false
410
+ chunkCSSMap = new Map ( )
412
411
emitTasks = [ ]
413
412
} ,
414
413
@@ -700,10 +699,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
700
699
chunkCSS = resolveAssetUrlsInCss ( chunkCSS , cssBundleName )
701
700
// finalizeCss is called for the aggregated chunk in generateBundle
702
701
703
- outputToExtractedCSSMap . set (
704
- opts ,
705
- ( outputToExtractedCSSMap . get ( opts ) || '' ) + chunkCSS ,
706
- )
702
+ chunkCSSMap . set ( chunk . fileName , chunkCSS )
707
703
}
708
704
return null
709
705
} ,
@@ -785,8 +781,31 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
785
781
} )
786
782
}
787
783
788
- let extractedCss = outputToExtractedCSSMap . get ( opts )
789
- if ( extractedCss && ! hasEmitted ) {
784
+ function extractCss ( ) {
785
+ let css = ''
786
+ const collected = new Set < OutputChunk > ( )
787
+ const prelimaryNameToChunkMap = new Map (
788
+ Object . values ( bundle )
789
+ . filter ( ( chunk ) : chunk is OutputChunk => chunk . type === 'chunk' )
790
+ . map ( ( chunk ) => [ chunk . preliminaryFileName , chunk ] ) ,
791
+ )
792
+
793
+ function collect ( fileName : string ) {
794
+ const chunk = bundle [ fileName ]
795
+ if ( ! chunk || chunk . type !== 'chunk' || collected . has ( chunk ) ) return
796
+ collected . add ( chunk )
797
+
798
+ chunk . imports . forEach ( collect )
799
+ css += chunkCSSMap . get ( chunk . preliminaryFileName ) ?? ''
800
+ }
801
+
802
+ for ( const chunkName of chunkCSSMap . keys ( ) )
803
+ collect ( prelimaryNameToChunkMap . get ( chunkName ) ?. fileName ?? '' )
804
+
805
+ return css
806
+ }
807
+ let extractedCss = ! hasEmitted && extractCss ( )
808
+ if ( extractedCss ) {
790
809
hasEmitted = true
791
810
extractedCss = await finalizeCss ( extractedCss , true , config )
792
811
this . emitFile ( {
0 commit comments