@@ -167,6 +167,7 @@ class MiniCssExtractPlugin {
167
167
result . push ( {
168
168
render : ( ) =>
169
169
this . renderContentAsset (
170
+ chunk ,
170
171
renderedModules ,
171
172
compilation . runtimeTemplate . requestShortener
172
173
) ,
@@ -191,6 +192,7 @@ class MiniCssExtractPlugin {
191
192
result . push ( {
192
193
render : ( ) =>
193
194
this . renderContentAsset (
195
+ chunk ,
194
196
renderedModules ,
195
197
compilation . runtimeTemplate . requestShortener
196
198
) ,
@@ -379,8 +381,42 @@ class MiniCssExtractPlugin {
379
381
return obj ;
380
382
}
381
383
382
- renderContentAsset ( modules , requestShortener ) {
383
- modules . sort ( ( a , b ) => a . index2 - b . index2 ) ;
384
+ getCssModulesRecursiveHelper ( module ) {
385
+ return module . dependencies
386
+ . filter ( dependency => dependency . module )
387
+ . map ( ( dependency ) => {
388
+ if ( dependency . module instanceof CssModule ) {
389
+ return dependency . module ;
390
+ } else if ( dependency . module . dependencies ) {
391
+ return this . getCssModulesRecursiveHelper ( dependency . module ) ;
392
+ }
393
+ return [ ] ;
394
+ } )
395
+ . reduce ( ( flattenedModules , dependency ) => {
396
+ if ( Array . isArray ( dependency ) ) {
397
+ dependency . forEach ( ( dep ) => { if ( dep ) flattenedModules . push ( dep ) ; } ) ;
398
+ } else if ( dependency ) {
399
+ flattenedModules . push ( dependency ) ;
400
+ }
401
+ return flattenedModules ;
402
+ } , [ ] ) ;
403
+ }
404
+
405
+ getCssModulesOrderMap ( chunk ) {
406
+ const [ shallowestModule ] = Array . from ( chunk . modulesIterable ) . sort ( ( a , b ) => a . depth - b . depth ) ;
407
+
408
+ return this . getCssModulesRecursiveHelper ( shallowestModule )
409
+ . reduce ( ( indexMap , module , i ) => {
410
+ // Only keep the first occurrence
411
+ if ( module . id in indexMap ) return indexMap ;
412
+ return Object . assign ( indexMap , { [ module . id ] : i } ) ;
413
+ } , { } ) ;
414
+ }
415
+
416
+ renderContentAsset ( chunk , modules , requestShortener ) {
417
+ const idToIndexMap = this . getCssModulesOrderMap ( chunk ) ;
418
+ modules . sort ( ( a , b ) => idToIndexMap [ a . id ] - idToIndexMap [ b . id ] ) ;
419
+
384
420
const source = new ConcatSource ( ) ;
385
421
const externalsSource = new ConcatSource ( ) ;
386
422
for ( const m of modules ) {
0 commit comments