55"use strict" ;
66
77const Queue = require ( "../util/Queue" ) ;
8+ const ChunkGroup = require ( "../ChunkGroup" ) ;
89
910const getParentChunksWithModule = ( currentChunk , module ) => {
1011 const chunks = [ ] ;
@@ -27,35 +28,75 @@ class RemoveParentModulesPlugin {
2728 apply ( compiler ) {
2829 compiler . hooks . compilation . tap ( "RemoveParentModulesPlugin" , ( compilation ) => {
2930 const handler = ( chunks ) => {
30- const queue = new Queue ( chunks ) ;
31+ const queue = new Queue ( ) ;
3132 const availableModulesMap = new Map ( ) ;
3233
3334 for ( const chunk of chunks ) {
3435 // initialize available modules for chunks without parents
35- if ( chunk . getNumberOfParents ( ) === 0 )
36+ if ( chunk . getNumberOfParents ( ) === 0 ) {
3637 availableModulesMap . set ( chunk , new Set ( ) ) ;
38+ for ( const child of chunk . chunksIterable )
39+ queue . enqueue ( child ) ;
40+ }
3741 }
3842
3943 while ( queue . length > 0 ) {
4044 const chunk = queue . dequeue ( ) ;
4145 let availableModules = availableModulesMap . get ( chunk ) ;
4246 let changed = false ;
43- for ( const parent of chunk . parentsIterable ) {
44- const availableModulesInParent = availableModulesMap . get ( parent ) ;
45- if ( availableModulesInParent !== undefined ) {
46- // If we know the available modules in parent: process these
47- if ( availableModules === undefined ) {
48- // if we have not own info yet: create new entry
49- availableModules = new Set ( availableModulesInParent ) ;
50- for ( const m of parent . modulesIterable )
51- availableModules . add ( m ) ;
52- availableModulesMap . set ( chunk , availableModules ) ;
53- changed = true ;
54- } else {
55- for ( const m of availableModules ) {
56- if ( ! parent . containsModule ( m ) && ! availableModulesInParent . has ( m ) ) {
57- availableModules . delete ( m ) ;
58- changed = true ;
47+ if ( chunk instanceof ChunkGroup ) {
48+ let allParentAvailableModules = new Set ( ) ;
49+ for ( const parent of chunk . parentsIterable ) {
50+ const availableModulesInParent = availableModulesMap . get ( parent ) ;
51+ if ( availableModulesInParent === undefined ) {
52+ allParentAvailableModules = undefined ;
53+ break ;
54+ }
55+ for ( const module of availableModulesInParent )
56+ allParentAvailableModules . add ( module ) ;
57+ }
58+ if ( availableModules === undefined ) {
59+ // if we have not own info yet: create new entry
60+ availableModules = allParentAvailableModules ;
61+ availableModulesMap . set ( chunk , availableModules ) ;
62+ changed = true ;
63+ } else {
64+ for ( const m of availableModules ) {
65+ if ( ! allParentAvailableModules . has ( m ) ) {
66+ availableModules . delete ( m ) ;
67+ changed = true ;
68+ }
69+ }
70+ }
71+ } else {
72+ for ( const parent of chunk . parentsIterable ) {
73+ const availableModulesInParent = availableModulesMap . get ( parent ) ;
74+ if ( availableModulesInParent !== undefined ) {
75+ // If we know the available modules in parent: process these
76+ if ( availableModules === undefined ) {
77+ // if we have not own info yet: create new entry
78+ availableModules = new Set ( availableModulesInParent ) ;
79+ if ( ! ( parent instanceof ChunkGroup ) ) {
80+ for ( const m of parent . modulesIterable )
81+ availableModules . add ( m ) ;
82+ }
83+ availableModulesMap . set ( chunk , availableModules ) ;
84+ changed = true ;
85+ } else {
86+ if ( parent instanceof ChunkGroup ) {
87+ for ( const m of availableModules ) {
88+ if ( ! availableModulesInParent . has ( m ) ) {
89+ availableModules . delete ( m ) ;
90+ changed = true ;
91+ }
92+ }
93+ } else {
94+ for ( const m of availableModules ) {
95+ if ( ! parent . containsModule ( m ) && ! availableModulesInParent . has ( m ) ) {
96+ availableModules . delete ( m ) ;
97+ changed = true ;
98+ }
99+ }
59100 }
60101 }
61102 }
0 commit comments