Code coverage report for lib\Chunk.js

Statements: 90.3% (121 / 134)      Branches: 67.24% (39 / 58)      Functions: 88.89% (32 / 36)      Lines: 90.3% (121 / 134)      Ignored: none     

All files » lib\ » Chunk.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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220        1 878 878 878 878 878 878 878 878 878 878 878 878 873             1   1 5129 1955   3174 3174     1   1 286     1       1       1 2 954 6   948 88   860 860       1   1   1 407     407 407     1 22     1   125     125 128 128 128   128 4     125 2 2 2   2 4     125 135 135       1 22       22 22 32 32 32   22 1 44 42 42 42   42     22 22 2     22 22 20       22 22 22 22 22   22 22 22 22       22   22     1 877     1 731 731 731 731 2996       1 8 8   8 15   15   8     1 26     26         26     1   4       4 4   4 4 7 4       4 12   12   4     1    
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
function Chunk(name, module, loc) {
	this.id = null;
	this.ids = null;
	this.name = name;
	this.modules = [];
	this.chunks = [];
	this.parents = [];
	this.blocks = [];
	this.origins = [];
	this.rendered = false;
	this.entry = false;
	this.initial = false;
	if(module) {
		this.origins.push({
			module: module,
			loc: loc,
			name: name
		});
	}
}
module.exports = Chunk;
 
Chunk.prototype.addModule = function(module) {
	if(this.modules.indexOf(module) >= 0) {
		return false;
	}
	this.modules.push(module);
	return true;
};
 
Chunk.prototype._removeAndDo = require("./removeAndDo");
 
Chunk.prototype.removeModule = function(module) {
	this._removeAndDo("modules", module, "removeChunk");
};
 
Chunk.prototype.removeChunk = function(chunk) {
	this._removeAndDo("chunks", chunk, "removeParent");
};
 
Chunk.prototype.removeParent = function(chunk) {
	this._removeAndDo("parents", chunk, "removeChunk");
};
 
function createAdder(collection) {
	return function(chunk) {
		if(chunk === this) {
			return false;
		}
		if(this[collection].indexOf(chunk) >= 0) {
			return false;
		}
		this[collection].push(chunk);
		return true;
	};
}
 
Chunk.prototype.addChunk = createAdder("chunks");
 
Chunk.prototype.addParent = createAdder("parents");
 
Chunk.prototype.addBlock = function(block) {
	Iif(this.blocks.indexOf(block) >= 0) {
		return false;
	}
	this.blocks.push(block);
	return true;
};
 
Chunk.prototype.addOrigin = function(module, loc) {
	this.origins.push({module: module, loc: loc, name: this.name});
};
 
Chunk.prototype.remove = function(reason) {
	// console.log("remove " + this.toString());
	this.modules.slice().forEach(function(m) {
		m.removeChunk(this);
	}, this);
	this.parents.forEach(function(c) {
		var idx = c.chunks.indexOf(this);
		Eif(idx >= 0) {
			c.chunks.splice(idx, 1);
		}
		this.chunks.forEach(function(cc) {
			cc.addParent(c);
		});
	}, this);
	this.chunks.forEach(function(c) {
		var idx = c.parents.indexOf(this);
		Eif(idx >= 0) {
			c.parents.splice(idx, 1);
		}
		this.parents.forEach(function(cc) {
			cc.addChunk(c);
		});
	}, this);
	this.blocks.forEach(function(b) {
		b.chunk = null;
		b.chunkReason = reason;
	}, this);
};
 
Chunk.prototype.integrate = function(other, reason) {
	Iif(!this.canBeIntegrated(other)) {
		return false;
	}
 
	var otherModules = other.modules.slice();
	otherModules.forEach(function(m) {
		m.removeChunk(other);
		m.addChunk(this);
		this.addModule(m);
	}, this);
	other.modules.length = 0;
	function moveChunks(chunks, kind, onChunk) {
		chunks.forEach(function(c) {
			var idx = c[kind].indexOf(other);
			Eif(idx >= 0) {
				c[kind].splice(idx, 1);
			}
			onChunk(c);
		});
	}
	moveChunks(other.parents, "chunks", function(c) {
		if(c !== this && this.addParent(c)) {
			c.addChunk(this);
		}
	}.bind(this));
	other.parents.length = 0;
	moveChunks(other.chunks, "parents", function(c) {
		Iif(c !== this && this.addChunk(c)) {
			c.addParent(this);
		}	
	}.bind(this));
	other.chunks.length = 0;
	other.blocks.forEach(function(b) {
		b.chunk = this;
		b.chunkReason = reason;
		this.addBlock(b);
	}, this);
	other.blocks.length = 0;
	other.origins.forEach(function(origin) {
		Eif(!origin.reasons) {
			origin.reasons = [reason];
		} else if(origin.reasons[0] !== reason) {
			origin.reasons.unshift(reason);
		}
		this.origins.push(origin);
	}, this);
	return true;
};
 
Chunk.prototype.isEmpty = function() {
	return (this.modules.length === 0);
};
 
Chunk.prototype.updateHash = function(hash) {
	hash.update(this.id + " ");
	hash.update(this.ids ? this.ids.join(",") : "");
	hash.update(this.name + "");
	this.modules.forEach(function(m) {
		m.updateHash(hash);
	});
};
 
Chunk.prototype.size = function(options) {
	var CHUNK_OVERHEAD = options.chunkOverhead || 10000;
	var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;
 
	var modulesSize = this.modules.map(function(m) {
		return m.size();
	}).reduce(function(a, b) {
		return a + b;
	}, 0);
	return modulesSize * (this.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
};
 
Chunk.prototype.canBeIntegrated = function(other) {
	Iif(other.initial) {
		return false;
	}
	Iif(this.initial) {
		if(other.parents.length !== 1 || other.parents[0] !== this) {
			return false;
		}
	}
	return true;
};
 
Chunk.prototype.integratedSize = function(other, options) {
	// Chunk if it's possible to integrate this chunks
	Iif(!this.canBeIntegrated(other)) {
		return false;
	}
 
	var CHUNK_OVERHEAD = options.chunkOverhead || 10000;
	var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;
 
	var mergedModules = this.modules.slice();
	other.modules.forEach(function(m) {
		if(this.modules.indexOf(m) < 0) {
			mergedModules.push(m);
		}
	}, this);
 
	var modulesSize = mergedModules.map(function(m) {
		return m.size();
	}).reduce(function(a, b) {
		return a + b;
	}, 0);
	return modulesSize * (this.initial || other.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
};
 
Chunk.prototype.toString = function() {
	return "Chunk[" + this.modules.join() + "]";
};