Skip to content

Commit bcc10e8

Browse files
committed
extract the last step of reconnecting the chunks to own methods
1 parent 9f18ea9 commit bcc10e8

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

lib/optimize/CommonsChunkPlugin.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,44 @@ The available options are:
205205
}
206206
}
207207

208+
connectUsedChunkAndCommonChunk(usedChunks, commonChunk) {
209+
for(let chunk of usedChunks) {
210+
// set commonChunk as new sole parent
211+
chunk.parents = [commonChunk];
212+
// add chunk to commonChunk
213+
commonChunk.addChunk(chunk);
214+
215+
for(let entrypoint of chunk.entrypoints) {
216+
entrypoint.insertChunk(commonChunk, chunk);
217+
}
218+
}
219+
}
220+
221+
connectChunkBlocksWithCommonChunk(chunks, commonChunk) {
222+
for(let chunk of chunks) {
223+
// only for non initial chunks
224+
// TODO: why?
225+
if(!chunk.isInitial()) {
226+
for(let block of chunk.blocks) {
227+
block.chunks.unshift(commonChunk);
228+
commonChunk.addBlock(block);
229+
}
230+
}
231+
}
232+
}
233+
234+
getAsyncChunkOrigin(chunks) {
235+
const origins = [];
236+
for(let chunk of chunks) {
237+
for(let origin of chunk.origins) {
238+
const newOrigin = Object.create(origin);
239+
newOrigin.reasons = (origin.reasons || []).concat("async commons");
240+
origins.push(newOrigin);
241+
}
242+
}
243+
return origins;
244+
}
245+
208246
apply(compiler) {
209247
const filenameTemplate = this.filenameTemplate;
210248
const asyncOption = this.async;
@@ -246,36 +284,17 @@ The available options are:
246284

247285
this.connectModulesWithCommonChunk(commonChunk, reallyUsedModules);
248286

249-
if(asyncOption) {
250-
for(let chunk of reallyUsedChunks) {
251-
if(chunk.isInitial()) continue;
252-
chunk.blocks.forEach((block) => {
253-
block.chunks.unshift(commonChunk);
254-
commonChunk.addBlock(block);
255-
});
256-
}
257-
asyncChunk.origins = Array.from(reallyUsedChunks).map((chunk) => {
258-
return chunk.origins.map((origin) => {
259-
const newOrigin = Object.create(origin);
260-
newOrigin.reasons = (origin.reasons || []).slice();
261-
newOrigin.reasons.push("async commons");
262-
return newOrigin;
263-
});
264-
}).reduce((arr, a) => {
265-
arr.push.apply(arr, a);
266-
return arr;
267-
}, []);
268-
} else {
269-
usedChunks.forEach((chunk) => {
270-
chunk.parents = [commonChunk];
271-
chunk.entrypoints.forEach((ep) => {
272-
ep.insertChunk(commonChunk, chunk);
273-
});
274-
commonChunk.addChunk(chunk);
275-
});
276-
}
287+
// set filenameTemplate for chunk
277288
if(filenameTemplate)
278289
commonChunk.filenameTemplate = filenameTemplate;
290+
291+
if(asyncOption) {
292+
this.connectChunkBlocksWithCommonChunk(reallyUsedChunks, commonChunk);
293+
asyncChunk.origins = this.getAsyncChunkOrigin(reallyUsedChunks);
294+
return;
295+
}
296+
297+
this.connectUsedChunkAndCommonChunk(usedChunks, commonChunk);
279298
});
280299
return true;
281300
});

0 commit comments

Comments
 (0)