Skip to content

Commit 779dbd4

Browse files
committed
more features for the CommonsChunkPlugin
* allow to select non-entry chunks * allow to select multiple/all commons chunks
1 parent 4fef131 commit 779dbd4

1 file changed

Lines changed: 72 additions & 43 deletions

File tree

lib/optimize/CommonsChunkPlugin.js

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,97 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function CommonsChunkPlugin(chunkName, filenameTemplate, entryPoints, minCount) {
6-
if(typeof filenameTemplate !== "string") {
7-
minCount = entryPoints;
8-
entryPoints = filenameTemplate
9-
filenameTemplate = chunkName;
5+
function CommonsChunkPlugin(chunkNames, filenameTemplate, selectedChunks, minCount) {
6+
if(typeof filenameTemplate !== "string" && filenameTemplate !== null) {
7+
minCount = selectedChunks;
8+
selectedChunks = filenameTemplate
9+
filenameTemplate = chunkNames;
1010
}
11-
if(typeof entryPoints === "number") {
12-
minCount = entryPoints;
13-
entryPoints = undefined;
11+
if(!Array.isArray(selectedChunks) && typeof selectedChunks !== "boolean") {
12+
minCount = selectedChunks;
13+
selectedChunks = undefined;
1414
}
15-
this.chunkName = chunkName;
15+
this.chunkNames = chunkNames;
1616
this.filenameTemplate = filenameTemplate;
1717
this.minCount = minCount;
18-
this.entryPoints = entryPoints;
18+
this.selectedChunks = selectedChunks;
1919
}
2020
module.exports = CommonsChunkPlugin;
2121
CommonsChunkPlugin.prototype.apply = function(compiler) {
22-
var chunkName = this.chunkName;
22+
var chunkNames = this.chunkNames;
2323
var filenameTemplate = this.filenameTemplate;
2424
var minCount = this.minCount;
25-
var entryPoints = this.entryPoints;
25+
var selectedChunks = this.selectedChunks;
2626
compiler.plugin("this-compilation", function(compilation) {
2727
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], function(chunks) {
28-
var commonModulesCount = [];
29-
var commonModules = [];
30-
var commonChunk = chunks.filter(function(chunk) {
31-
return chunk.name === chunkName;
32-
})[0] || this.addChunk(chunkName);
33-
var usedChunks = chunks.filter(function(chunk) {
34-
if(chunk === commonChunk) return false;
35-
if(!chunk.entry) return false;
36-
if(!entryPoints) return true;
37-
return entryPoints.indexOf(chunk.name) >= 0;
38-
});
39-
usedChunks.forEach(function(chunk) {
40-
chunk.modules.forEach(function(module) {
41-
var idx = commonModules.indexOf(module);
42-
if(idx < 0) {
43-
commonModules.push(module);
44-
commonModulesCount.push(1);
45-
} else {
46-
commonModulesCount[idx]++;
47-
}
28+
if(!chunkNames && selectedChunks === false) {
29+
var commonChunks = chunks;
30+
} else if(Array.isArray(chunkNames)) {
31+
var commonChunks = chunks.filter(function(chunk) {
32+
return chunkNames.indexOf(chunk.name) >= 0;
4833
});
49-
});
50-
commonModulesCount.forEach(function(count, idx) {
51-
if(count >= (minCount || Math.max(2, usedChunks.length))) {
34+
} else {
35+
var commonChunks = chunks.filter(function(chunk) {
36+
return chunk.name === chunkNames;
37+
});
38+
}
39+
if(commonChunks.length === 0) {
40+
var chunk = this.addChunk(chunkNames);
41+
chunk.initial = chunk.entry = true;
42+
commonChunks = [chunk];
43+
}
44+
commonChunks.forEach(function processCommonChunk(commonChunk) {
45+
var commonModulesCount = [];
46+
var commonModules = [];
47+
if(Array.isArray(selectedChunks)) {
48+
var usedChunks = chunks.filter(function(chunk) {
49+
if(chunk === commonChunk) return false;
50+
return selectedChunks.indexOf(chunk.name) >= 0;
51+
});
52+
} else if(selectedChunks === false || !commonChunk.entry) {
53+
var usedChunks = (commonChunk.chunks || []).filter(function(chunk) {
54+
// we can only move modules from this chunk if the "commonChunk" is the only parent
55+
return chunk.parents.length === 1;
56+
});
57+
} else {
58+
var usedChunks = chunks.filter(function(chunk) {
59+
if(chunk === commonChunk) return false;
60+
return chunk.entry;
61+
});
62+
}
63+
usedChunks.forEach(function(chunk) {
64+
chunk.modules.forEach(function(module) {
65+
var idx = commonModules.indexOf(module);
66+
if(idx < 0) {
67+
commonModules.push(module);
68+
commonModulesCount.push(1);
69+
} else {
70+
commonModulesCount[idx]++;
71+
}
72+
});
73+
});
74+
commonModulesCount.forEach(function(count, idx) {
5275
var module = commonModules[idx];
76+
if(typeof minCount === "function") {
77+
if(!minCount(module, count))
78+
return;
79+
} else if(count < (minCount || Math.max(2, usedChunks.length))) {
80+
return;
81+
}
5382
usedChunks.forEach(function(chunk) {
5483
module.removeChunk(chunk);
5584
});
5685
commonChunk.addModule(module);
5786
module.addChunk(commonChunk);
58-
}
59-
});
60-
usedChunks.forEach(function(chunk) {
61-
chunk.parents = [commonChunk];
62-
commonChunk.chunks.push(chunk);
63-
chunk.entry = false;
87+
});
88+
usedChunks.forEach(function(chunk) {
89+
chunk.parents = [commonChunk];
90+
commonChunk.chunks.push(chunk);
91+
chunk.entry = false;
92+
});
93+
if(filenameTemplate)
94+
commonChunk.filenameTemplate = filenameTemplate;
6495
});
65-
commonChunk.initial = commonChunk.entry = true;
66-
commonChunk.filenameTemplate = filenameTemplate;
6796
});
6897
});
6998
};

0 commit comments

Comments
 (0)