Skip to content

Commit 063c54f

Browse files
committed
extract creation of usedChunks
1 parent d53c882 commit 063c54f

1 file changed

Lines changed: 41 additions & 19 deletions

File tree

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,42 @@ The available options are:
9494
throw new Error("Invalid chunkNames argument");
9595
}
9696

97+
getUsedChunks(compilation, allChunks, commonChunk, commonChunks, currentIndex, selectedChunks, isAsync) {
98+
const asyncOrNoSelectedChunk = selectedChunks === false || isAsync;
99+
100+
if(Array.isArray(selectedChunks)) {
101+
return allChunks.filter(chunk => {
102+
const notCommmonChunk = chunk !== commonChunk;
103+
const isSelectedChunk = selectedChunks.indexOf(chunk.name) > -1;
104+
return notCommmonChunk && isSelectedChunk;
105+
});
106+
}
107+
108+
if(asyncOrNoSelectedChunk) {
109+
// nothing to do here
110+
if(!commonChunk.chunks) {
111+
return [];
112+
}
113+
114+
return commonChunk.chunks.filter((chunk) => {
115+
// we can only move modules from this chunk if the "commonChunk" is the only parent
116+
return isAsync || chunk.parents.length === 1;
117+
});
118+
}
119+
120+
// this is an entry point - bad
121+
if(commonChunk.parents.length > 0) {
122+
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
123+
return;
124+
}
125+
126+
return allChunks.filter((chunk) => {
127+
const found = commonChunks.indexOf(chunk);
128+
if(found >= currentIndex) return false;
129+
return chunk.hasRuntime();
130+
});
131+
}
132+
97133
apply(compiler) {
98134
const filenameTemplate = this.filenameTemplate;
99135
const minChunks = this.minChunks;
@@ -109,25 +145,11 @@ The available options are:
109145

110146
const commonChunks = this.formCommonChunks(chunks, compilation);
111147

112-
commonChunks.forEach(function processCommonChunk(commonChunk, idx) {
113-
let usedChunks;
114-
if(Array.isArray(selectedChunks)) {
115-
usedChunks = chunks.filter(chunk => chunk !== commonChunk && selectedChunks.indexOf(chunk.name) >= 0);
116-
} else if(selectedChunks === false || asyncOption) {
117-
usedChunks = (commonChunk.chunks || []).filter((chunk) => {
118-
// we can only move modules from this chunk if the "commonChunk" is the only parent
119-
return asyncOption || chunk.parents.length === 1;
120-
});
121-
} else {
122-
if(commonChunk.parents.length > 0) {
123-
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
124-
return;
125-
}
126-
usedChunks = chunks.filter((chunk) => {
127-
const found = commonChunks.indexOf(chunk);
128-
if(found >= idx) return false;
129-
return chunk.hasRuntime();
130-
});
148+
commonChunks.forEach((commonChunk, idx) => {
149+
const usedChunks = this.getUsedChunks(compilation, chunks, commonChunk, commonChunks, idx, this.selectedChunks, this.async);
150+
// bail as this is an erronous state
151+
if(!usedChunks) {
152+
return;
131153
}
132154
let asyncChunk;
133155
if(asyncOption) {

0 commit comments

Comments
 (0)