@@ -138,10 +138,58 @@ The available options are:
138138 return asyncChunk ;
139139 }
140140
141+ // If minChunks is a function use that
142+ // otherwhise check if a module is used at least minChunks or 2 or usedChunks.length time
143+ getModuleFilter ( minChunks , commonChunk , usedChunksLength ) {
144+ if ( typeof minChunks === "function" ) {
145+ return minChunks ;
146+ }
147+ const minCount = ( minChunks || Math . max ( 2 , usedChunksLength ) ) ;
148+ const isUsedAtLeastMinTimes = ( module , count ) => count >= minCount ;
149+ return isUsedAtLeastMinTimes ;
150+ }
151+
152+ getReallyUsedModules ( minChunks , usedChunks , commonChunk ) {
153+ if ( minChunks === Infinity ) {
154+ return [ ] ;
155+ }
156+
157+ // count how many chunks contain a module
158+ const commonModulesToCountMap = usedChunks . reduce ( ( map , chunk ) => {
159+ for ( let module of chunk . modules ) {
160+ let count = map . has ( module ) ? map . get ( module ) : 0 ;
161+ map . set ( module , count + 1 ) ;
162+ }
163+ return map ;
164+ } , new Map ( ) ) ;
165+
166+ // filter by minChunks
167+ const moduleFilterCount = this . getModuleFilter ( minChunks , commonChunk , usedChunks . length ) ;
168+ // filter by condition
169+ const moduleFilterCondition = ( module , chunk ) => {
170+ if ( ! module . chunkCondition ) {
171+ return true ;
172+ }
173+ return module . chunkCondition ( chunk ) ;
174+ } ;
175+
176+ return Array . from ( commonModulesToCountMap ) . filter ( entry => {
177+ const module = entry [ 0 ] ;
178+ const count = entry [ 1 ] ;
179+ // if the module passes both filters, keep it.
180+ return moduleFilterCount ( module , count ) && moduleFilterCondition ( module , commonChunk ) ;
181+ } ) . map ( entry => entry [ 0 ] ) ;
182+ }
183+
184+ getModulesSize ( modules ) {
185+ return modules . reduce ( ( count , module ) => count + module . size ( ) , 0 ) ;
186+ }
187+
141188 apply ( compiler ) {
142189 const filenameTemplate = this . filenameTemplate ;
143190 const asyncOption = this . async ;
144191 const minSize = this . minSize ;
192+ const minChunks = this . minChunks ;
145193 const ident = this . ident ;
146194 compiler . plugin ( "this-compilation" , ( compilation ) => {
147195 compilation . plugin ( [ "optimize-chunks" , "optimize-extracted-chunks" ] , ( chunks ) => {
@@ -164,42 +212,16 @@ The available options are:
164212 commonChunk . addChunk ( asyncChunk ) ;
165213 commonChunk = asyncChunk ;
166214 }
167- const reallyUsedModules = [ ] ;
168- if ( minChunks !== Infinity ) {
169- const commonModulesCount = [ ] ;
170- const commonModules = [ ] ;
171- usedChunks . forEach ( ( chunk ) => {
172- chunk . modules . forEach ( ( module ) => {
173- const idx = commonModules . indexOf ( module ) ;
174- if ( idx < 0 ) {
175- commonModules . push ( module ) ;
176- commonModulesCount . push ( 1 ) ;
177- } else {
178- commonModulesCount [ idx ] ++ ;
179- }
180- } ) ;
181- } ) ;
182- const _minChunks = ( minChunks || Math . max ( 2 , usedChunks . length ) ) ;
183- commonModulesCount . forEach ( ( count , idx ) => {
184- const module = commonModules [ idx ] ;
185- if ( typeof minChunks === "function" ) {
186- if ( ! minChunks ( module , count ) )
187- return ;
188- } else if ( count < _minChunks ) {
189- return ;
190- }
191- if ( module . chunkCondition && ! module . chunkCondition ( commonChunk ) )
192- return ;
193- reallyUsedModules . push ( module ) ;
194- } ) ;
195- }
215+ const reallyUsedModules = this . getReallyUsedModules ( minChunks , usedChunks , commonChunk ) ;
216+
217+ // check if the extracted modules would be big enough to be extraced
196218 if ( minSize ) {
197- const size = reallyUsedModules . reduce ( ( a , b ) => {
198- return a + b . size ( ) ;
199- } , 0 ) ;
200- if ( size < minSize )
219+ const modulesSize = this . getModulesSize ( reallyUsedModules ) ;
220+ // if too small, bail
221+ if ( modulesSize < minSize )
201222 return ;
202223 }
224+
203225 const reallyUsedChunks = new Set ( ) ;
204226 reallyUsedModules . forEach ( ( module ) => {
205227 usedChunks . forEach ( ( chunk ) => {
0 commit comments