@@ -39,6 +39,13 @@ class AggressiveSplittingPlugin {
3939 apply ( compiler ) {
4040 compiler . plugin ( "this-compilation" , ( compilation ) => {
4141 compilation . plugin ( "optimize-chunks-advanced" , ( chunks ) => {
42+ // Precompute stuff
43+ const nameToModuleMap = new Map ( ) ;
44+ compilation . modules . forEach ( m => {
45+ const name = identifierUtils . makePathsRelative ( compiler . context , m . identifier ( ) , compilation . cache ) ;
46+ nameToModuleMap . set ( name , m ) ;
47+ } ) ;
48+
4249 const savedSplits = compilation . records && compilation . records . aggressiveSplits || [ ] ;
4350 const usedSplits = compilation . _aggressiveSplittingSplits ?
4451 savedSplits . concat ( compilation . _aggressiveSplittingSplits ) : savedSplits ;
@@ -48,45 +55,51 @@ class AggressiveSplittingPlugin {
4855 // 1. try to restore to recorded splitting
4956 for ( let j = 0 ; j < usedSplits . length ; j ++ ) {
5057 const splitData = usedSplits [ j ] ;
51- for ( let i = 0 ; i < chunks . length ; i ++ ) {
52- const chunk = chunks [ i ] ;
58+ const selectedModules = splitData . modules . map ( name => nameToModuleMap . get ( name ) ) ;
5359
54- if ( chunk . getNumberOfModules ( ) < splitData . modules . length )
55- continue ;
60+ // Does the modules exist at all?
61+ if ( selectedModules . every ( Boolean ) ) {
5662
57- const nameToModuleMap = new Map ( ) ;
58- chunk . forEachModule ( m => {
59- const name = identifierUtils . makePathsRelative ( compiler . context , m . identifier ( ) , compilation . cache ) ;
60- nameToModuleMap . set ( name , m ) ;
61- } ) ;
63+ // Find all chunks containing all modules in the split
64+ for ( let i = 0 ; i < chunks . length ; i ++ ) {
65+ const chunk = chunks [ i ] ;
6266
63- const selectedModules = splitData . modules . map ( name => nameToModuleMap . get ( name ) ) ;
64- if ( selectedModules . every ( Boolean ) ) {
65- if ( chunk . getNumberOfModules ( ) > splitData . modules . length ) {
66- const newChunk = compilation . addChunk ( ) ;
67- selectedModules . forEach ( moveModuleBetween ( chunk , newChunk ) ) ;
68- chunk . split ( newChunk ) ;
69- chunk . name = null ;
70- newChunk . _fromAggressiveSplitting = true ;
71- if ( j < savedSplits . length )
72- newChunk . _fromAggressiveSplittingIndex = j ;
73- if ( splitData . id !== null && splitData . id !== undefined ) {
74- newChunk . id = splitData . id ;
75- }
76- newChunk . origins = chunk . origins . map ( copyWithReason ) ;
77- chunk . origins = chunk . origins . map ( copyWithReason ) ;
78- return true ;
79- } else {
80- if ( j < savedSplits . length )
81- chunk . _fromAggressiveSplittingIndex = j ;
82- chunk . name = null ;
83- if ( splitData . id !== null && splitData . id !== undefined ) {
84- chunk . id = splitData . id ;
67+ // Cheap check if chunk is suitable at all
68+ if ( chunk . getNumberOfModules ( ) < splitData . modules . length )
69+ continue ;
70+
71+ // Check if all modules are in the chunk
72+ if ( selectedModules . every ( m => chunk . containsModule ( m ) ) ) {
73+
74+ // Is chunk identical to the split or do we need to split it?
75+ if ( chunk . getNumberOfModules ( ) > splitData . modules . length ) {
76+ // split the chunk into two parts
77+ const newChunk = compilation . addChunk ( ) ;
78+ selectedModules . forEach ( moveModuleBetween ( chunk , newChunk ) ) ;
79+ chunk . split ( newChunk ) ;
80+ chunk . name = null ;
81+ newChunk . _fromAggressiveSplitting = true ;
82+ if ( j < savedSplits . length )
83+ newChunk . _fromAggressiveSplittingIndex = j ;
84+ if ( splitData . id !== null && splitData . id !== undefined ) {
85+ newChunk . id = splitData . id ;
86+ }
87+ newChunk . origins = chunk . origins . map ( copyWithReason ) ;
88+ chunk . origins = chunk . origins . map ( copyWithReason ) ;
89+ return true ;
90+ } else { // chunk is identical to the split
91+ if ( j < savedSplits . length )
92+ chunk . _fromAggressiveSplittingIndex = j ;
93+ chunk . name = null ;
94+ if ( splitData . id !== null && splitData . id !== undefined ) {
95+ chunk . id = splitData . id ;
96+ }
8597 }
8698 }
8799 }
88100 }
89101 }
102+
90103 // 2. for any other chunk which isn't splitted yet, split it
91104 for ( let i = 0 ; i < chunks . length ; i ++ ) {
92105 const chunk = chunks [ i ] ;
0 commit comments