| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | function MinChunkSizePlugin(options) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 	if(typeof options !== "object" || Array.isArray(options)) { | 
					
						
							| 
									
										
										
										
											2015-07-21 06:29:53 +08:00
										 |  |  | 		throw new Error("Argument should be an options object.\nFor more info on options, see https://webpack.github.io/docs/list-of-plugins.html"); | 
					
						
							| 
									
										
										
										
											2015-05-28 00:46:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	this.options = options; | 
					
						
							| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = MinChunkSizePlugin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MinChunkSizePlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 	var options = this.options; | 
					
						
							|  |  |  | 	var minChunkSize = options.minChunkSize; | 
					
						
							| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | 	compiler.plugin("compilation", function(compilation) { | 
					
						
							|  |  |  | 		compilation.plugin("optimize-chunks", function(chunks) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 			var combinations = []; | 
					
						
							|  |  |  | 			chunks.forEach(function(a, idx) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				for(var i = 0; i < idx; i++) { | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 					var b = chunks[i]; | 
					
						
							|  |  |  | 					combinations.push([b, a]); | 
					
						
							| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 			var equalOptions = { | 
					
						
							|  |  |  | 				chunkOverhead: 1, | 
					
						
							|  |  |  | 				entryChunkMultiplicator: 1 | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			combinations = combinations.filter(function(pair) { | 
					
						
							|  |  |  | 				return pair[0].size(equalOptions) < minChunkSize || pair[1].size(equalOptions) < minChunkSize; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			combinations.forEach(function(pair) { | 
					
						
							|  |  |  | 				var a = pair[0].size(options); | 
					
						
							|  |  |  | 				var b = pair[1].size(options); | 
					
						
							|  |  |  | 				var ab = pair[0].integratedSize(pair[1], options); | 
					
						
							|  |  |  | 				pair.unshift(a + b - ab, ab); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 			combinations = combinations.filter(function(pair) { | 
					
						
							|  |  |  | 				return pair[1] !== false; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 			if(combinations.length === 0) return; | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 			combinations.sort(function(a, b) { | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 				var diff = b[0] - a[0]; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(diff !== 0) return diff; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 				return a[1] - b[1]; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var pair = combinations[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pair[2].integrate(pair[3], "min-size"); | 
					
						
							|  |  |  | 			chunks.splice(chunks.indexOf(pair[3]), 1); | 
					
						
							|  |  |  | 			this.restartApplyPlugins(); | 
					
						
							| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |