@@ -33,35 +33,48 @@ module.exports = class AutomaticCommonsChunksPlugin {
3333 apply ( compiler ) {
3434 compiler . hooks . compilation . tap ( "AutomaticCommonsChunksPlugin" , compilation => {
3535 compilation . hooks . optimizeChunksAdvanced . tap ( "AutomaticCommonsChunksPlugin" , chunks => {
36+ // Give each selected chunk an index (to create strings from chunks)
3637 const indexMap = new Map ( ) ;
3738 let index = 1 ;
3839 for ( const chunk of chunks ) {
3940 if ( chunk . isInitial ( ) === this . options . initialChunks )
4041 indexMap . set ( chunk , index ++ ) ;
4142 }
43+ // Map a list of chunks to a list of modules
44+ // For the key the chunk "index" is used, the value is a SortableSet of modules
4245 const chunksModulesMap = new Map ( ) ;
46+ // Map a list of chunks to a name (not every list of chunks is mapped, only when "name" option is used)
4347 const chunksNameMap = new Map ( ) ;
48+ // Walk through all modules
4449 for ( const module of compilation . modules ) {
50+ // Get indices of chunks in which this module occurs
4551 const chunkIndices = Array . from ( module . chunksIterable , chunk => indexMap . get ( chunk ) ) . filter ( Boolean ) ;
52+ // Get name from "name" option
4653 let name = this . options . name ;
4754 if ( typeof name === "function" )
4855 name = name ( module ) ;
4956 if ( name ) {
50- chunkIndices . push ( name ) ;
57+ chunkIndices . push ( `[ ${ name } ]` ) ;
5158 } else if ( this . options . onlyNamed ) {
59+ // May skip unnamed chunks if "onlyNamed" is used
5260 continue ;
5361 }
62+ // skip for modules which are only in one chunk or don't get a name
5463 if ( chunkIndices . length <= 1 ) continue ;
64+ // Create key for maps
5565 const key = chunkIndices . sort ( ) . join ( ) ;
66+ // Add module to maps
5667 let modules = chunksModulesMap . get ( key ) ;
5768 if ( modules === undefined ) {
5869 chunksModulesMap . set ( key , modules = new SortableSet ( undefined , sortByIdentifier ) ) ;
5970 if ( name ) {
71+ // Note name when used
6072 chunksNameMap . set ( key , name ) ;
6173 }
6274 }
6375 modules . add ( module ) ;
6476 }
77+ // Get size of module lists and sort them by name and size
6578 const entries = Array . from ( chunksModulesMap . entries ( ) , pair => {
6679 const modules = pair [ 1 ] ;
6780 const size = Array . from ( modules , m => m . size ( ) ) . reduce ( ( a , b ) => a + b , 0 ) ;
@@ -71,9 +84,8 @@ module.exports = class AutomaticCommonsChunksPlugin {
7184 size
7285 } ;
7386 } ) . sort ( ( a , b ) => {
87+ // Sort
7488 // 1. by chunk name
75- // 2. by total modules size
76- // 3. by module identifiers
7789 const chunkNameA = chunksNameMap . get ( a . key ) ;
7890 const chunkNameB = chunksNameMap . get ( b . key ) ;
7991 if ( chunkNameA && ! chunkNameB ) return - 1 ;
@@ -82,10 +94,12 @@ module.exports = class AutomaticCommonsChunksPlugin {
8294 if ( chunkNameA < chunkNameB ) return - 1 ;
8395 if ( chunkNameA > chunkNameB ) return 1 ;
8496 }
97+ // 2. by total modules size
8598 const diffSize = b . size - a . size ;
8699 if ( diffSize ) return diffSize ;
87100 const modulesA = a . modules ;
88101 const modulesB = b . modules ;
102+ // 3. by module identifiers
89103 const diff = modulesA . size - modulesB . size ;
90104 if ( diff ) return diff ;
91105 modulesA . sort ( ) ;
@@ -102,44 +116,55 @@ module.exports = class AutomaticCommonsChunksPlugin {
102116 if ( aModuleIdentifier < bModuleIdentifier ) return 1 ;
103117 }
104118 } ) ;
119+
105120 let changed = false ;
106- for ( const { key, modules, size } of entries ) {
107- const chunkName = chunksNameMap . get ( key ) ;
108- if ( ! chunkName && size < this . options . minSize ) continue ;
109- const newChunk = compilation . addChunk ( chunkName ) ;
110- let splitted = false ;
111- const firstModule = modules . values ( ) . next ( ) . value ;
121+ // Walk though all entries
122+ for ( const item of entries ) {
123+ const chunkName = chunksNameMap . get ( item . key ) ;
124+ // Skip if size is smaller than minimum size
125+ if ( ! chunkName && item . size < this . options . minSize ) continue ;
126+ // Variable for the new chunk (lazy created)
127+ let newChunk ;
128+ // Walk through all chunks
129+ // All modules have the same chunks so we can use the first module
130+ const firstModule = item . modules . values ( ) . next ( ) . value ;
112131 for ( const chunk of firstModule . chunksIterable ) {
113- // skip itself when already a chunk of the module
114- if ( newChunk === chunk ) continue ;
132+ // skip if we address ourself
133+ if ( chunk . name === chunkName ) continue ;
115134 // only use selected chunks
116135 if ( ! indexMap . get ( chunk ) ) continue ;
117136 // respect max requests when not a named chunk
118137 if ( ! chunkName && getRequests ( chunk ) >= this . options . maxRequests ) continue ;
119- splitted = true ;
138+ if ( newChunk === undefined ) {
139+ // Create the new chunk
140+ newChunk = compilation . addChunk ( chunkName ) ;
141+ }
142+ // Add graph connections for splitted chunk
120143 chunk . split ( newChunk ) ;
121- for ( const module of modules ) {
144+ // Remove all selected modules from the chunk
145+ for ( const module of item . modules ) {
122146 chunk . removeModule ( module ) ;
123147 module . rewriteChunkInReasons ( chunk , [ newChunk ] ) ;
124148 }
125149 }
126- if ( splitted ) {
150+ // If we successfully creates a new chunk
151+ if ( newChunk ) {
152+ // If the choosen name is already an entry point we remove the entry point
127153 if ( chunkName ) {
128154 const entrypoint = compilation . entrypoints [ chunkName ] ;
129155 if ( entrypoint ) {
130156 delete compilation . entrypoints [ chunkName ] ;
131157 entrypoint . remove ( ) ;
132158 }
133159 }
160+ // Add a note to the chunk
134161 newChunk . chunkReason = chunkName ? "vendors chunk" : "commons chunk" ;
135- for ( const module of modules ) {
162+ // Add all modules to the new chunk
163+ for ( const module of item . modules ) {
136164 newChunk . addModule ( module ) ;
137165 module . addChunk ( newChunk ) ;
138166 }
139167 changed = true ;
140- } else if ( ! chunkName ) {
141- newChunk . remove ( "empty" ) ;
142- chunks . splice ( chunks . indexOf ( newChunk ) , 1 ) ;
143168 }
144169 }
145170 if ( changed ) return true ;
0 commit comments