Code coverage report for lib\optimize\CommonsChunkPlugin.js

Statements: 96.08% (49 / 51)      Branches: 93.75% (15 / 16)      Functions: 100% (10 / 10)      Lines: 95.83% (46 / 48)      Ignored: none     

All files » lib\optimize\ » CommonsChunkPlugin.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68        1 6 5 5 5   6       6 6 6 6   1 1 6 6 6 6 6 6 6 6 6 6 38 32 21 17   6 13 49 49 37 37   12       6 37 11 11 24   11 11     6 13 13 13   6 6        
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
function CommonsChunkPlugin(chunkName, filenameTemplate, entryPoints, minCount) {
	if(typeof filenameTemplate !== "string") {
		minCount = entryPoints;
		entryPoints = filenameTemplate
		filenameTemplate = chunkName;
	}
	Iif(typeof entryPoints === "number") {
		minCount = entryPoints;
		entryPoints = undefined;
	}
	this.chunkName = chunkName;
	this.filenameTemplate = filenameTemplate;
	this.minCount = minCount;
	this.entryPoints = entryPoints;
}
module.exports = CommonsChunkPlugin;
CommonsChunkPlugin.prototype.apply = function(compiler) {
	var chunkName = this.chunkName;
	var filenameTemplate = this.filenameTemplate;
	var minCount = this.minCount;
	var entryPoints = this.entryPoints;
	compiler.plugin("this-compilation", function(compilation) {
		compilation.plugin("optimize-chunks", function(chunks) {
			var commonModulesCount = [];
			var commonModules = [];
			var commonChunk = this.addChunk(chunkName);
			var usedChunks = chunks.filter(function(chunk) {
				if(chunk === commonChunk) return false;
				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]++;
					}
				});
			});
			commonModulesCount.forEach(function(count, idx) {
				if(count >= (minCount || Math.max(2, usedChunks.length))) {
					var module = commonModules[idx];
					usedChunks.forEach(function(chunk) {
						module.removeChunk(chunk);
					});
					commonChunk.addModule(module);
					module.addChunk(commonChunk);
				}
			});
			usedChunks.forEach(function(chunk) {
				chunk.parents = [commonChunk];
				commonChunk.chunks.push(chunk);
				chunk.entry = false;
			});
			commonChunk.initial = commonChunk.entry = true;
			commonChunk.filenameTemplate = filenameTemplate;
		});
	});
};