@@ -167,6 +167,7 @@ class MiniCssExtractPlugin {
167167 result . push ( {
168168 render : ( ) =>
169169 this . renderContentAsset (
170+ chunk ,
170171 renderedModules ,
171172 compilation . runtimeTemplate . requestShortener
172173 ) ,
@@ -191,6 +192,7 @@ class MiniCssExtractPlugin {
191192 result . push ( {
192193 render : ( ) =>
193194 this . renderContentAsset (
195+ chunk ,
194196 renderedModules ,
195197 compilation . runtimeTemplate . requestShortener
196198 ) ,
@@ -379,8 +381,42 @@ class MiniCssExtractPlugin {
379381 return obj ;
380382 }
381383
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+
384420 const source = new ConcatSource ( ) ;
385421 const externalsSource = new ConcatSource ( ) ;
386422 for ( const m of modules ) {
0 commit comments