Skip to content

Commit 9f18ea9

Browse files
committed
split up and extract default linking between modules and chunks
1 parent 24b8aeb commit 9f18ea9

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,26 @@ The available options are:
185185
return modules.reduce((count, module) => count + module.size(), 0);
186186
}
187187

188+
removeModulesFromUsedChunksAndReturnUsedChunks(reallyUsedModules, usedChunks) {
189+
return reallyUsedModules.reduce((affectedChunksSet, module) => {
190+
for(let chunk of usedChunks) {
191+
// removeChunk returns true if the chunk was contained and succesfully removed
192+
// false if the module did not have a connection to the chunk in question
193+
if(module.removeChunk(chunk)) {
194+
affectedChunksSet.add(chunk);
195+
}
196+
}
197+
return affectedChunksSet;
198+
}, new Set());
199+
}
200+
201+
connectModulesWithCommonChunk(chunk, modules) {
202+
for(let module of modules) {
203+
chunk.addModule(module);
204+
module.addChunk(chunk);
205+
}
206+
}
207+
188208
apply(compiler) {
189209
const filenameTemplate = this.filenameTemplate;
190210
const asyncOption = this.async;
@@ -222,16 +242,10 @@ The available options are:
222242
return;
223243
}
224244

225-
const reallyUsedChunks = new Set();
226-
reallyUsedModules.forEach((module) => {
227-
usedChunks.forEach((chunk) => {
228-
if(module.removeChunk(chunk)) {
229-
reallyUsedChunks.add(chunk);
230-
}
231-
});
232-
commonChunk.addModule(module);
233-
module.addChunk(commonChunk);
234-
});
245+
const reallyUsedChunks = this.removeModulesFromUsedChunksAndReturnUsedChunks(reallyUsedModules, usedChunks);
246+
247+
this.connectModulesWithCommonChunk(commonChunk, reallyUsedModules);
248+
235249
if(asyncOption) {
236250
for(let chunk of reallyUsedChunks) {
237251
if(chunk.isInitial()) continue;

0 commit comments

Comments
 (0)