@@ -21,31 +21,31 @@ class LimitChunkCountPlugin {
2121 if ( chunks . length <= maxChunks ) return ;
2222
2323 if ( chunks . length > maxChunks ) {
24- let combinations = [ ] ;
25- chunks . forEach ( ( a , idx ) => {
24+ const sortedExtendedPairCombinations = chunks . reduce ( ( combinations , a , idx ) => {
25+ // create combination pairs
2626 for ( let i = 0 ; i < idx ; i ++ ) {
2727 const b = chunks [ i ] ;
2828 combinations . push ( [ b , a ] ) ;
2929 }
30- } ) ;
31-
32- combinations . forEach ( ( pair ) => {
30+ return combinations ;
31+ } , [ ] ) . map ( ( pair ) => {
32+ // extend combination pairs with size and integrated size
3333 const a = pair [ 0 ] . size ( options ) ;
3434 const b = pair [ 1 ] . size ( options ) ;
3535 const ab = pair [ 0 ] . integratedSize ( pair [ 1 ] , options ) ;
36- pair . unshift ( a + b - ab , ab ) ;
37- pair . push ( a , b ) ;
38- } ) ;
39- combinations = combinations . filter ( ( pair ) => {
40- return pair [ 1 ] !== false ;
41- } ) ;
42- combinations . sort ( ( a , b ) => {
36+ return [ a + b - ab , ab , pair [ 0 ] , pair [ 1 ] , a , b ] ;
37+ } ) . filter ( ( extendedPair ) => {
38+ // filter pairs that do not have an integratedSize
39+ // meaning they can NOT be integrated!
40+ return extendedPair [ 1 ] !== false ;
41+ } ) . sort ( ( a , b ) => { // sadly javascript does an inplace sort here
42+ // sort them by size
4343 const diff = b [ 0 ] - a [ 0 ] ;
4444 if ( diff !== 0 ) return diff ;
4545 return a [ 1 ] - b [ 1 ] ;
4646 } ) ;
4747
48- const pair = combinations [ 0 ] ;
48+ const pair = sortedExtendedPairCombinations [ 0 ] ;
4949
5050 if ( pair && pair [ 2 ] . integrate ( pair [ 3 ] , "limit" ) ) {
5151 chunks . splice ( chunks . indexOf ( pair [ 3 ] ) , 1 ) ;
0 commit comments