Skip to content

Commit 20e9f65

Browse files
committed
accept an array of entryPoints for Commons chunk
1 parent 153be56 commit 20e9f65

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,39 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function CommonsChunkPlugin(filenameTemplate, minCount) {
5+
function CommonsChunkPlugin(filenameTemplate, entryPoints, minCount) {
6+
if(typeof entryPoints === "number") {
7+
minCount = entryPoints;
8+
entryPoints = undefined;
9+
}
610
this.filenameTemplate = filenameTemplate;
7-
this.minCount = minCount || 2;
11+
this.minCount = minCount || (entryPoints ? entryPoints.length : 2);
12+
this.entryPoints = entryPoints;
813
}
914
module.exports = CommonsChunkPlugin;
1015
CommonsChunkPlugin.prototype.apply = function(compiler) {
1116
var filenameTemplate = this.filenameTemplate;
1217
var minCount = this.minCount;
18+
var entryPoints = this.entryPoints;
1319
compiler.plugin("compilation", function(compilation) {
1420
compilation.plugin("optimize-chunks", function(chunks) {
1521
var commonModulesCount = [];
1622
var commonModules = [];
17-
chunks.forEach(function(chunk) {
18-
if(chunk.entry) {
19-
chunk.modules.forEach(function(module) {
20-
var idx = commonModules.indexOf(module);
21-
if(idx < 0) {
22-
commonModules.push(module);
23-
commonModulesCount.push(1);
24-
} else {
25-
commonModulesCount[idx]++;
26-
}
27-
});
28-
}
23+
var usedChunks = chunks.filter(function(chunk) {
24+
if(!chunk.entry) return false;
25+
if(!entryPoints) return true;
26+
return entryPoints.indexOf(chunk.name) >= 0;
27+
});
28+
usedChunks.forEach(function(chunk) {
29+
chunk.modules.forEach(function(module) {
30+
var idx = commonModules.indexOf(module);
31+
if(idx < 0) {
32+
commonModules.push(module);
33+
commonModulesCount.push(1);
34+
} else {
35+
commonModulesCount[idx]++;
36+
}
37+
});
2938
});
3039
var commonChunk = this.addChunk();
3140
commonModulesCount.forEach(function(count, idx) {
@@ -38,12 +47,10 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
3847
module.addChunk(commonChunk);
3948
}
4049
});
41-
chunks.forEach(function(chunk) {
42-
if(chunk.entry) {
43-
chunk.parents = [commonChunk];
44-
commonChunk.chunks.push(chunk);
45-
chunk.entry = false;
46-
}
50+
usedChunks.forEach(function(chunk) {
51+
chunk.parents = [commonChunk];
52+
commonChunk.chunks.push(chunk);
53+
chunk.entry = false;
4754
});
4855
commonChunk.entry = true;
4956
commonChunk.id = 0;

0 commit comments

Comments
 (0)