22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Tobias Koppers @sokra
44*/
5- function hasModule ( chunk , module , checkedChunks ) {
6- if ( chunk . modules . indexOf ( module ) >= 0 ) return [ chunk ] ;
7- if ( chunk . entry ) return false ;
8- return allHaveModule ( chunk . parents . filter ( function ( c ) {
9- return checkedChunks . indexOf ( c ) < 0 ;
10- } ) , module , checkedChunks ) ;
5+ function listToSet ( list , chunk ) {
6+ var set = { } ;
7+ list . forEach ( function ( module ) {
8+ set [ module . debugId ] = {
9+ module : module ,
10+ chunks : [ chunk ]
11+ } ;
12+ } ) ;
13+ return set ;
14+ }
15+
16+ function mergeSets ( a , b ) {
17+ var newSet = { } ;
18+ Object . keys ( a ) . forEach ( function ( key ) {
19+ var item = a [ key ] ;
20+ newSet [ key ] = {
21+ module : item . module ,
22+ chunks : item . chunks
23+ } ;
24+ } ) ;
25+ Object . keys ( b ) . forEach ( function ( key ) {
26+ var item = b [ key ] ;
27+ newSet [ key ] = {
28+ module : item . module ,
29+ chunks : item . chunks
30+ } ;
31+ } ) ;
32+ return newSet ;
33+ }
34+
35+ function intersectSets ( a , b ) {
36+ var newSet = { } ;
37+ Object . keys ( a ) . forEach ( function ( key ) {
38+ var aItem = a [ key ] ;
39+ var bItem = b [ key ] ;
40+ if ( bItem ) {
41+ newSet [ key ] = {
42+ module : aItem . module ,
43+ chunks : aItem . chunks . concat ( bItem . chunks )
44+ } ;
45+ }
46+ } ) ;
47+ return newSet ;
1148}
1249
13- function allHaveModule ( someChunks , module , checkedChunks ) {
14- if ( ! checkedChunks ) checkedChunks = [ ] ;
15- var chunks = [ ] ;
16- for ( var i = 0 ; i < someChunks . length ; i ++ ) {
17- checkedChunks . push ( someChunks [ i ] ) ;
18- var subChunks = hasModule ( someChunks [ i ] , module , checkedChunks ) ;
19- if ( ! subChunks ) return false ;
20- addToSet ( chunks , subChunks ) ;
21- }
22- return chunks ;
50+ function intersectAll ( map ) {
51+ var keys = Object . keys ( map ) ;
52+ if ( keys . length === 0 )
53+ return null ;
54+ return keys . map ( function ( key ) {
55+ return map [ key ] ;
56+ } ) . reduce ( intersectSets ) ;
2357}
2458
2559function addToSet ( set , items ) {
@@ -29,16 +63,62 @@ function addToSet(set, items) {
2963 } ) ;
3064}
3165
66+ function toStr ( set ) {
67+ return Object . keys ( set ) . map ( function ( key ) {
68+ return set [ key ] . module . request . substr ( - 12 ) ;
69+ } ) . join ( ", " ) ;
70+ }
71+
3272function RemoveParentModulesPlugin ( ) { }
3373module . exports = RemoveParentModulesPlugin ;
3474
3575RemoveParentModulesPlugin . prototype . apply = function ( compiler ) {
3676 compiler . plugin ( "compilation" , function ( compilation ) {
3777 compilation . plugin ( [ "optimize-chunks-basic" , "optimize-extracted-chunks-basic" ] , function ( chunks ) {
78+ var todo = chunks . slice ( ) . reverse ( ) ;
79+ todo . forEach ( function ( chunk , idx ) {
80+ chunk . _RemoveParentModulesPlugin_processed = false ;
81+ chunk . _RemoveParentModulesPlugin_availableModulesByChunk = { } ;
82+ chunk . _RemoveParentModulesPlugin_index = idx ;
83+ } )
84+ for ( var i = 0 ; i < todo . length ; i ++ ) {
85+ var chunk = todo [ i ] ;
86+ var index = chunk . _RemoveParentModulesPlugin_index ;
87+ var availableModules = chunk . _RemoveParentModulesPlugin_availableModules = intersectAll ( chunk . _RemoveParentModulesPlugin_availableModulesByChunk ) ;
88+ if ( chunk . chunks . length === 0 ) {
89+ chunk . _RemoveParentModulesPlugin_processed = true ;
90+ continue ;
91+ }
92+ var set = listToSet ( chunk . modules ) ;
93+ if ( availableModules )
94+ set = mergeSets ( set , availableModules ) ;
95+ chunk . chunks . forEach ( function ( child ) {
96+ var availableModules = child . _RemoveParentModulesPlugin_availableModulesByChunk [ index ] ;
97+ child . _RemoveParentModulesPlugin_availableModulesByChunk [ index ] = set ;
98+ if ( ! availableModules || Object . keys ( availableModules ) . length !== Object . keys ( set ) . length ) {
99+ if ( child . _RemoveParentModulesPlugin_processed ) {
100+ todo . push ( child ) ;
101+ child . _RemoveParentModulesPlugin_processed = false ;
102+ }
103+ }
104+ } ) ;
105+ chunk . _RemoveParentModulesPlugin_processed = true ;
106+ }
38107 chunks . forEach ( function ( chunk ) {
108+ var availableModules = chunk . _RemoveParentModulesPlugin_availableModules ;
109+ delete chunk . _RemoveParentModulesPlugin_availableModulesByChunk ;
110+ delete chunk . _RemoveParentModulesPlugin_availableModules ;
111+ delete chunk . _RemoveParentModulesPlugin_processed ;
112+ delete chunk . _RemoveParentModulesPlugin_index ;
113+ if ( ! availableModules ) return ;
39114 chunk . modules . slice ( ) . forEach ( function ( module ) {
40115 if ( chunk . entry ) return ;
41- var parentChunksWithModule = allHaveModule ( chunk . parents , module ) ;
116+ var info = availableModules [ module . debugId ] ;
117+ if ( ! info ) return ;
118+ var parentChunksWithModule = info . chunks ;
119+ parentChunksWithModule = parentChunksWithModule . filter ( function ( chunk , idx ) {
120+ return parentChunksWithModule . indexOf ( chunk ) === idx ;
121+ } ) ;
42122 if ( parentChunksWithModule ) {
43123 module . rewriteChunkInReasons ( chunk , parentChunksWithModule ) ;
44124 chunk . removeModule ( module ) ;
0 commit comments