| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | var compareLocations = require("./compareLocations"); | 
					
						
							| 
									
										
										
										
											2016-04-11 05:55:37 +08:00
										 |  |  | var debugId = 1000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-23 22:31:40 +08:00
										 |  |  | function Chunk(name, module, loc) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.id = null; | 
					
						
							| 
									
										
										
										
											2013-05-08 19:28:54 +08:00
										 |  |  | 	this.ids = null; | 
					
						
							| 
									
										
										
										
											2016-04-11 05:55:37 +08:00
										 |  |  | 	this.debugId = debugId++; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.name = name; | 
					
						
							|  |  |  | 	this.modules = []; | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	this.entrypoints = []; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.chunks = []; | 
					
						
							|  |  |  | 	this.parents = []; | 
					
						
							|  |  |  | 	this.blocks = []; | 
					
						
							| 
									
										
										
										
											2014-01-23 22:31:40 +08:00
										 |  |  | 	this.origins = []; | 
					
						
							| 
									
										
										
										
											2015-01-30 07:44:58 +08:00
										 |  |  | 	this.files = []; | 
					
						
							| 
									
										
										
										
											2013-05-08 19:28:54 +08:00
										 |  |  | 	this.rendered = false; | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 	if(module) { | 
					
						
							|  |  |  | 		this.origins.push({ | 
					
						
							|  |  |  | 			module: module, | 
					
						
							|  |  |  | 			loc: loc, | 
					
						
							|  |  |  | 			name: name | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = Chunk; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | Object.defineProperty(Chunk.prototype, "entry", { | 
					
						
							|  |  |  | 	configurable: false, | 
					
						
							|  |  |  | 	get: function() { | 
					
						
							|  |  |  | 		throw new Error("Chunk.entry was removed. Use hasRuntime()"); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	set: function() { | 
					
						
							|  |  |  | 		throw new Error("Chunk.entry was removed. Use hasRuntime()"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Object.defineProperty(Chunk.prototype, "initial", { | 
					
						
							|  |  |  | 	configurable: false, | 
					
						
							|  |  |  | 	get: function() { | 
					
						
							|  |  |  | 		throw new Error("Chunk.initial was removed. Use isInitial()"); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	set: function() { | 
					
						
							|  |  |  | 		throw new Error("Chunk.initial was removed. Use isInitial()"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.hasRuntime = function() { | 
					
						
							|  |  |  | 	if(this.entrypoints.length === 0) return false; | 
					
						
							|  |  |  | 	return this.entrypoints[0].chunks[0] === this; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.isInitial = function() { | 
					
						
							|  |  |  | 	return this.entrypoints.length > 0; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.hasEntryModule = function() { | 
					
						
							|  |  |  | 	return !!this.entryModule; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | Chunk.prototype.addModule = function(module) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 	if(this.modules.indexOf(module) >= 0) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.modules.push(module); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | Chunk.prototype._removeAndDo = require("./removeAndDo"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | Chunk.prototype.removeModule = function(module) { | 
					
						
							|  |  |  | 	this._removeAndDo("modules", module, "removeChunk"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.removeChunk = function(chunk) { | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 	this._removeAndDo("chunks", chunk, "removeParent"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.removeParent = function(chunk) { | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 	this._removeAndDo("parents", chunk, "removeChunk"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 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"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | Chunk.prototype.addBlock = function(block) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 	if(this.blocks.indexOf(block) >= 0) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.blocks.push(block); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-24 20:32:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.addOrigin = function(module, loc) { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 	this.origins.push({ | 
					
						
							|  |  |  | 		module: module, | 
					
						
							|  |  |  | 		loc: loc, | 
					
						
							|  |  |  | 		name: this.name | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-24 20:32:58 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.remove = function(reason) { | 
					
						
							|  |  |  | 	this.modules.slice().forEach(function(m) { | 
					
						
							|  |  |  | 		m.removeChunk(this); | 
					
						
							|  |  |  | 	}, this); | 
					
						
							|  |  |  | 	this.parents.forEach(function(c) { | 
					
						
							|  |  |  | 		var idx = c.chunks.indexOf(this); | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(idx >= 0) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			c.chunks.splice(idx, 1); | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		this.chunks.forEach(function(cc) { | 
					
						
							|  |  |  | 			cc.addParent(c); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, this); | 
					
						
							|  |  |  | 	this.chunks.forEach(function(c) { | 
					
						
							|  |  |  | 		var idx = c.parents.indexOf(this); | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(idx >= 0) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			c.parents.splice(idx, 1); | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		this.parents.forEach(function(cc) { | 
					
						
							|  |  |  | 			cc.addChunk(c); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, this); | 
					
						
							|  |  |  | 	this.blocks.forEach(function(b) { | 
					
						
							| 
									
										
										
										
											2015-01-12 06:15:11 +08:00
										 |  |  | 		var idx = b.chunks.indexOf(this); | 
					
						
							|  |  |  | 		if(idx >= 0) { | 
					
						
							|  |  |  | 			b.chunks.splice(idx, 1); | 
					
						
							|  |  |  | 			if(b.chunks.length === 0) { | 
					
						
							|  |  |  | 				b.chunks = null; | 
					
						
							|  |  |  | 				b.chunkReason = reason; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	}, this); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | Chunk.prototype.moveModule = function(module, other) { | 
					
						
							|  |  |  | 	module.removeChunk(this); | 
					
						
							|  |  |  | 	module.addChunk(other); | 
					
						
							|  |  |  | 	other.addModule(module); | 
					
						
							|  |  |  | 	module.rewriteChunkInReasons(this, [other]); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | Chunk.prototype.integrate = function(other, reason) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 	if(!this.canBeIntegrated(other)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	var otherModules = other.modules.slice(); | 
					
						
							|  |  |  | 	otherModules.forEach(function(m) { | 
					
						
							|  |  |  | 		m.removeChunk(other); | 
					
						
							|  |  |  | 		m.addChunk(this); | 
					
						
							|  |  |  | 		this.addModule(m); | 
					
						
							| 
									
										
										
										
											2014-09-08 04:54:38 +08:00
										 |  |  | 		m.rewriteChunkInReasons(other, [this]); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	}, this); | 
					
						
							|  |  |  | 	other.modules.length = 0; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 	function moveChunks(chunks, kind, onChunk) { | 
					
						
							|  |  |  | 		chunks.forEach(function(c) { | 
					
						
							|  |  |  | 			var idx = c[kind].indexOf(other); | 
					
						
							|  |  |  | 			if(idx >= 0) { | 
					
						
							|  |  |  | 				c[kind].splice(idx, 1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			onChunk(c); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	moveChunks(other.parents, "chunks", function(c) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(c !== this && this.addParent(c)) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			c.addChunk(this); | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	other.parents.length = 0; | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 	moveChunks(other.chunks, "parents", function(c) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(c !== this && this.addChunk(c)) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			c.addParent(this); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	other.chunks.length = 0; | 
					
						
							|  |  |  | 	other.blocks.forEach(function(b) { | 
					
						
							| 
									
										
										
										
											2015-01-02 04:58:27 +08:00
										 |  |  | 		b.chunks = (b.chunks || [this]).map(function(c) { | 
					
						
							|  |  |  | 			return c === other ? this : c; | 
					
						
							|  |  |  | 		}, this); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		b.chunkReason = reason; | 
					
						
							|  |  |  | 		this.addBlock(b); | 
					
						
							|  |  |  | 	}, this); | 
					
						
							|  |  |  | 	other.blocks.length = 0; | 
					
						
							| 
									
										
										
										
											2014-01-23 22:31:40 +08:00
										 |  |  | 	other.origins.forEach(function(origin) { | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 		this.origins.push(origin); | 
					
						
							|  |  |  | 	}, this); | 
					
						
							|  |  |  | 	this.origins.forEach(function(origin) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(!origin.reasons) { | 
					
						
							|  |  |  | 			origin.reasons = [reason]; | 
					
						
							|  |  |  | 		} else if(origin.reasons[0] !== reason) { | 
					
						
							|  |  |  | 			origin.reasons.unshift(reason); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-07-16 03:59:24 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 	this.chunks = this.chunks.filter(function(c) { | 
					
						
							|  |  |  | 		return c !== other && c !== this; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	this.parents = this.parents.filter(function(c) { | 
					
						
							|  |  |  | 		return c !== other && c !== this; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | Chunk.prototype.split = function(newChunk) { | 
					
						
							|  |  |  | 	var _this = this; | 
					
						
							|  |  |  | 	this.blocks.forEach(function(b) { | 
					
						
							|  |  |  | 		newChunk.blocks.push(b); | 
					
						
							|  |  |  | 		b.chunks.push(newChunk); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	this.chunks.forEach(function(c) { | 
					
						
							|  |  |  | 		newChunk.chunks.push(c); | 
					
						
							|  |  |  | 		c.parents.push(newChunk); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	this.parents.forEach(function(p) { | 
					
						
							|  |  |  | 		p.chunks.push(newChunk); | 
					
						
							|  |  |  | 		newChunk.parents.push(p); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	this.entrypoints.forEach(function(e) { | 
					
						
							|  |  |  | 		e.insertChunk(newChunk, _this); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | Chunk.prototype.isEmpty = function() { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:39:56 +08:00
										 |  |  | 	return this.modules.length === 0; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.updateHash = function(hash) { | 
					
						
							| 
									
										
										
										
											2013-05-08 19:28:54 +08:00
										 |  |  | 	hash.update(this.id + " "); | 
					
						
							|  |  |  | 	hash.update(this.ids ? this.ids.join(",") : ""); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	hash.update((this.name || "") + " "); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.modules.forEach(function(m) { | 
					
						
							|  |  |  | 		m.updateHash(hash); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | Chunk.prototype.size = function(options) { | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	var CHUNK_OVERHEAD = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 11:02:07 +08:00
										 |  |  | 	var modulesSize = this.modules.reduce(function(a, b) { | 
					
						
							|  |  |  | 		return a + b.size(); | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	}, 0); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	return modulesSize * (this.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | Chunk.prototype.canBeIntegrated = function(other) { | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	if(other.isInitial()) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	if(this.isInitial()) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(other.parents.length !== 1 || other.parents[0] !== this) { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | Chunk.prototype.integratedSize = function(other, options) { | 
					
						
							| 
									
										
										
										
											2015-05-21 05:57:10 +08:00
										 |  |  | 	// Chunk if it's possible to integrate this chunk
 | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 	if(!this.canBeIntegrated(other)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 18:37:28 +08:00
										 |  |  | 	var CHUNK_OVERHEAD = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var mergedModules = this.modules.slice(); | 
					
						
							|  |  |  | 	other.modules.forEach(function(m) { | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		if(this.modules.indexOf(m) < 0) { | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 			mergedModules.push(m); | 
					
						
							| 
									
										
										
										
											2014-06-04 03:03:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	}, this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-07 06:11:13 +08:00
										 |  |  | 	var modulesSize = mergedModules.reduce(function(a, m) { | 
					
						
							|  |  |  | 		return a + m.size(); | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	}, 0); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	return modulesSize * (this.isInitial() || other.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-23 04:34:25 +08:00
										 |  |  | Chunk.prototype.getChunkMaps = function(includeEntries, realHash) { | 
					
						
							| 
									
										
										
										
											2014-11-02 19:48:15 +08:00
										 |  |  | 	var chunksProcessed = []; | 
					
						
							|  |  |  | 	var chunkHashMap = {}; | 
					
						
							|  |  |  | 	var chunkNameMap = {}; | 
					
						
							|  |  |  | 	(function addChunk(c) { | 
					
						
							|  |  |  | 		if(chunksProcessed.indexOf(c) >= 0) return; | 
					
						
							|  |  |  | 		chunksProcessed.push(c); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 		if(!c.hasRuntime() || includeEntries) { | 
					
						
							| 
									
										
										
										
											2015-07-23 04:34:25 +08:00
										 |  |  | 			chunkHashMap[c.id] = realHash ? c.hash : c.renderedHash; | 
					
						
							| 
									
										
										
										
											2014-11-02 19:48:15 +08:00
										 |  |  | 			if(c.name) | 
					
						
							|  |  |  | 				chunkNameMap[c.id] = c.name; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		c.chunks.forEach(addChunk); | 
					
						
							|  |  |  | 	}(this)); | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		hash: chunkHashMap, | 
					
						
							|  |  |  | 		name: chunkNameMap | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-11-02 19:48:15 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | function byId(a, b) { | 
					
						
							|  |  |  | 	return a.id - b.id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.sortItems = function() { | 
					
						
							|  |  |  | 	this.modules.sort(byId); | 
					
						
							|  |  |  | 	this.origins.sort(function(a, b) { | 
					
						
							|  |  |  | 		var aIdent = a.module.identifier(); | 
					
						
							|  |  |  | 		var bIdent = b.module.identifier(); | 
					
						
							|  |  |  | 		if(aIdent < bIdent) return -1; | 
					
						
							|  |  |  | 		if(aIdent > bIdent) return 1; | 
					
						
							|  |  |  | 		return compareLocations(a.loc, b.loc); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	this.origins.forEach(function(origin) { | 
					
						
							|  |  |  | 		if(origin.reasons) | 
					
						
							|  |  |  | 			origin.reasons.sort(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | Chunk.prototype.toString = function() { | 
					
						
							|  |  |  | 	return "Chunk[" + this.modules.join() + "]"; | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-07-16 03:59:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Chunk.prototype.checkConstraints = function() { | 
					
						
							|  |  |  | 	var chunk = this; | 
					
						
							|  |  |  | 	chunk.chunks.forEach(function(child, idx) { | 
					
						
							|  |  |  | 		if(chunk.chunks.indexOf(child) !== idx) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:41:26 +08:00
										 |  |  | 			throw new Error("checkConstraints: duplicate child in chunk " + chunk.debugId + " " + child.debugId); | 
					
						
							| 
									
										
										
										
											2016-07-16 03:59:24 +08:00
										 |  |  | 		if(child.parents.indexOf(chunk) < 0) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:41:26 +08:00
										 |  |  | 			throw new Error("checkConstraints: child missing parent " + chunk.debugId + " -> " + child.debugId); | 
					
						
							| 
									
										
										
										
											2016-07-16 03:59:24 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 	chunk.parents.forEach(function(parent, idx) { | 
					
						
							|  |  |  | 		if(chunk.parents.indexOf(parent) !== idx) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:41:26 +08:00
										 |  |  | 			throw new Error("checkConstraints: duplicate parent in chunk " + chunk.debugId + " " + parent.debugId); | 
					
						
							| 
									
										
										
										
											2016-07-16 03:59:24 +08:00
										 |  |  | 		if(parent.chunks.indexOf(chunk) < 0) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:41:26 +08:00
										 |  |  | 			throw new Error("checkConstraints: parent missing child " + parent.debugId + " <- " + chunk.debugId); | 
					
						
							| 
									
										
										
										
											2016-07-16 03:59:24 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; |