@@ -13,13 +13,39 @@ function AggressiveSplittingPlugin(options) {
1313}
1414module . exports = AggressiveSplittingPlugin ;
1515
16- function makeRelative ( compiler , identifier ) {
17- var context = compiler . context ;
18- return identifier . split ( "|" ) . map ( function ( str ) {
19- return str . split ( "!" ) . map ( function ( str ) {
20- return path . relative ( context , str ) ;
21- } ) . join ( "!" ) ;
22- } ) . join ( "|" ) ;
16+ function makeRelative ( context ) {
17+ return function ( module ) {
18+ var identifier = module . identifier ( ) ;
19+ return identifier . split ( "|" ) . map ( function ( str ) {
20+ return str . split ( "!" ) . map ( function ( str ) {
21+ return path . relative ( context , str ) ;
22+ } ) . join ( "!" ) ;
23+ } ) . join ( "|" ) ;
24+ }
25+ }
26+
27+ function toIndexOf ( list ) {
28+ return function ( item ) {
29+ return list . indexOf ( item ) ;
30+ }
31+ }
32+
33+ function toChunkModuleIndices ( modules ) {
34+ return function ( idx ) {
35+ return modules [ idx ] ;
36+ }
37+ }
38+
39+ function moveModuleBetween ( oldChunk , newChunk ) {
40+ return function ( module ) {
41+ oldChunk . moveModule ( module , newChunk ) ;
42+ }
43+ }
44+
45+ function isNotAEntryModule ( entryModule ) {
46+ return function ( module ) {
47+ return entryModule !== module ;
48+ }
2349}
2450
2551function copyWithReason ( obj ) {
@@ -48,26 +74,19 @@ AggressiveSplittingPlugin.prototype.apply = function(compiler) {
4874 var splitData = usedSplits [ j ] ;
4975 for ( i = 0 ; i < chunks . length ; i ++ ) {
5076 chunk = chunks [ i ] ;
51- var chunkModuleNames = chunk . modules . map ( function ( m ) {
52- return makeRelative ( compiler , m . identifier ( ) ) ;
53- } ) ;
77+ var chunkModuleNames = chunk . modules . map ( makeRelative ( compiler . context ) ) ;
78+
5479 if ( chunkModuleNames . length < splitData . modules )
5580 continue ;
56- var moduleIndicies = splitData . modules . map ( function ( m ) {
57- return chunkModuleNames . indexOf ( m ) ;
58- } ) ;
81+ var moduleIndicies = splitData . modules . map ( toIndexOf ( chunkModuleNames ) ) ;
5982 var hasAllModules = moduleIndicies . every ( function ( idx ) {
6083 return idx >= 0 ;
6184 } ) ;
6285 if ( hasAllModules ) {
6386 if ( chunkModuleNames . length > splitData . modules . length ) {
64- var selectedModules = moduleIndicies . map ( function ( idx ) {
65- return chunk . modules [ idx ] ;
66- } ) ;
87+ var selectedModules = moduleIndicies . map ( toChunkModuleIndices ( chunk . modules ) ) ;
6788 newChunk = compilation . addChunk ( ) ;
68- selectedModules . forEach ( function ( m ) {
69- chunk . moveModule ( m , newChunk ) ;
70- } ) ;
89+ selectedModules . forEach ( moveModuleBetween ( chunk , newChunk ) ) ;
7190 chunk . split ( newChunk ) ;
7291 chunk . name = null ;
7392 newChunk . _fromAggressiveSplitting = true ;
@@ -92,15 +111,15 @@ AggressiveSplittingPlugin.prototype.apply = function(compiler) {
92111 var size = chunk . size ( _this . options ) ;
93112 if ( size > maxSize && chunk . modules . length > 1 ) {
94113 newChunk = compilation . addChunk ( ) ;
95- var modules = chunk . modules . filter ( function ( m ) {
96- return chunk . entryModule !== m ;
97- } ) . sort ( function ( a , b ) {
98- a = a . identifier ( ) ;
99- b = b . identifier ( ) ;
100- if ( a > b ) return 1 ;
101- if ( a < b ) return - 1 ;
102- return 0 ;
103- } ) ;
114+ var modules = chunk . modules
115+ . filter ( isNotAEntryModule ( chunk . entryModule ) )
116+ . sort ( function ( a , b ) {
117+ a = a . identifier ( ) ;
118+ b = b . identifier ( ) ;
119+ if ( a > b ) return 1 ;
120+ if ( a < b ) return - 1 ;
121+ return 0 ;
122+ } ) ;
104123 for ( var k = 0 ; k < modules . length ; k ++ ) {
105124 chunk . moveModule ( modules [ k ] , newChunk ) ;
106125 var newSize = newChunk . size ( _this . options ) ;
@@ -126,9 +145,7 @@ AggressiveSplittingPlugin.prototype.apply = function(compiler) {
126145 newChunk . origins = chunk . origins . map ( copyWithReason ) ;
127146 chunk . origins = chunk . origins . map ( copyWithReason ) ;
128147 compilation . _aggressiveSplittingSplits = ( compilation . _aggressiveSplittingSplits || [ ] ) . concat ( {
129- modules : newChunk . modules . map ( function ( m ) {
130- return makeRelative ( compiler , m . identifier ( ) ) ;
131- } )
148+ modules : newChunk . modules . map ( makeRelative ( compiler . context ) )
132149 } ) ;
133150 return true ;
134151 } else {
@@ -146,9 +163,7 @@ AggressiveSplittingPlugin.prototype.apply = function(compiler) {
146163 if ( chunk . hasEntryModule ( ) ) return ;
147164 var size = chunk . size ( _this . options ) ;
148165 var incorrectSize = size < minSize ;
149- var modules = chunk . modules . map ( function ( m ) {
150- return makeRelative ( compiler , m . identifier ( ) ) ;
151- } ) ;
166+ var modules = chunk . modules . map ( makeRelative ( compiler . context ) ) ;
152167 if ( typeof chunk . _fromAggressiveSplittingIndex === "undefined" ) {
153168 if ( incorrectSize ) return ;
154169 chunk . recorded = true ;
0 commit comments