2013-12-03 16:27:15 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2014-07-19 20:32:48 +08:00
|
|
|
function CommonsChunkPlugin(chunkName, filenameTemplate, entryPoints, minCount) {
|
|
|
|
if(typeof filenameTemplate !== "string") {
|
|
|
|
minCount = entryPoints;
|
|
|
|
entryPoints = filenameTemplate
|
|
|
|
filenameTemplate = chunkName;
|
|
|
|
}
|
2013-12-03 18:28:39 +08:00
|
|
|
if(typeof entryPoints === "number") {
|
|
|
|
minCount = entryPoints;
|
|
|
|
entryPoints = undefined;
|
|
|
|
}
|
2014-07-19 20:32:48 +08:00
|
|
|
this.chunkName = chunkName;
|
2013-12-03 18:19:30 +08:00
|
|
|
this.filenameTemplate = filenameTemplate;
|
2013-12-03 18:44:46 +08:00
|
|
|
this.minCount = minCount;
|
2013-12-03 18:28:39 +08:00
|
|
|
this.entryPoints = entryPoints;
|
2013-12-03 16:27:15 +08:00
|
|
|
}
|
|
|
|
module.exports = CommonsChunkPlugin;
|
|
|
|
CommonsChunkPlugin.prototype.apply = function(compiler) {
|
2014-07-19 20:32:48 +08:00
|
|
|
var chunkName = this.chunkName;
|
2013-12-03 18:19:30 +08:00
|
|
|
var filenameTemplate = this.filenameTemplate;
|
2013-12-03 16:27:15 +08:00
|
|
|
var minCount = this.minCount;
|
2013-12-03 18:28:39 +08:00
|
|
|
var entryPoints = this.entryPoints;
|
2014-07-19 20:32:48 +08:00
|
|
|
compiler.plugin("this-compilation", function(compilation) {
|
2014-09-23 14:42:54 +08:00
|
|
|
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], function(chunks) {
|
2013-12-03 16:27:15 +08:00
|
|
|
var commonModulesCount = [];
|
|
|
|
var commonModules = [];
|
2014-09-23 14:42:54 +08:00
|
|
|
var commonChunk = chunks.filter(function(chunk) {
|
|
|
|
return chunk.name === chunkName;
|
|
|
|
})[0] || this.addChunk(chunkName);
|
2013-12-03 18:28:39 +08:00
|
|
|
var usedChunks = chunks.filter(function(chunk) {
|
2014-01-31 20:12:51 +08:00
|
|
|
if(chunk === commonChunk) return false;
|
2013-12-03 18:28:39 +08:00
|
|
|
if(!chunk.entry) return false;
|
|
|
|
if(!entryPoints) return true;
|
|
|
|
return entryPoints.indexOf(chunk.name) >= 0;
|
|
|
|
});
|
|
|
|
usedChunks.forEach(function(chunk) {
|
|
|
|
chunk.modules.forEach(function(module) {
|
|
|
|
var idx = commonModules.indexOf(module);
|
|
|
|
if(idx < 0) {
|
|
|
|
commonModules.push(module);
|
|
|
|
commonModulesCount.push(1);
|
|
|
|
} else {
|
|
|
|
commonModulesCount[idx]++;
|
|
|
|
}
|
|
|
|
});
|
2013-12-03 16:27:15 +08:00
|
|
|
});
|
|
|
|
commonModulesCount.forEach(function(count, idx) {
|
2014-07-24 18:47:18 +08:00
|
|
|
if(count >= (minCount || Math.max(2, usedChunks.length))) {
|
2013-12-03 16:27:15 +08:00
|
|
|
var module = commonModules[idx];
|
2013-12-03 18:34:38 +08:00
|
|
|
usedChunks.forEach(function(chunk) {
|
2013-12-03 16:27:15 +08:00
|
|
|
module.removeChunk(chunk);
|
|
|
|
});
|
|
|
|
commonChunk.addModule(module);
|
|
|
|
module.addChunk(commonChunk);
|
|
|
|
}
|
|
|
|
});
|
2013-12-03 18:28:39 +08:00
|
|
|
usedChunks.forEach(function(chunk) {
|
|
|
|
chunk.parents = [commonChunk];
|
|
|
|
commonChunk.chunks.push(chunk);
|
|
|
|
chunk.entry = false;
|
2013-12-03 16:27:15 +08:00
|
|
|
});
|
2013-12-04 06:11:14 +08:00
|
|
|
commonChunk.initial = commonChunk.entry = true;
|
2013-12-03 18:19:30 +08:00
|
|
|
commonChunk.filenameTemplate = filenameTemplate;
|
2013-12-03 16:27:15 +08:00
|
|
|
});
|
|
|
|
});
|
2013-12-04 00:14:28 +08:00
|
|
|
};
|