| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-05 12:17:12 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 17:19:28 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | const schema = require("../../schemas/plugins/optimize/LimitChunkCountPlugin.json"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 12:17:12 +08:00
										 |  |  | class LimitChunkCountPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | 		validateOptions(schema, options || {}, "Limit Chunk Count Plugin"); | 
					
						
							| 
									
										
										
										
											2017-01-05 12:17:12 +08:00
										 |  |  | 		this.options = options || {}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("LimitChunkCountPlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.optimizeChunksAdvanced.tap( | 
					
						
							|  |  |  | 				"LimitChunkCountPlugin", | 
					
						
							|  |  |  | 				chunks => { | 
					
						
							|  |  |  | 					const maxChunks = options.maxChunks; | 
					
						
							|  |  |  | 					if (!maxChunks) return; | 
					
						
							|  |  |  | 					if (maxChunks < 1) return; | 
					
						
							|  |  |  | 					if (chunks.length <= maxChunks) return; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const sortedExtendedPairCombinations = chunks | 
					
						
							|  |  |  | 						.reduce((combinations, a, idx) => { | 
					
						
							|  |  |  | 							// create combination pairs
 | 
					
						
							|  |  |  | 							for (let i = 0; i < idx; i++) { | 
					
						
							|  |  |  | 								const b = chunks[i]; | 
					
						
							|  |  |  | 								combinations.push([b, a]); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							return combinations; | 
					
						
							|  |  |  | 						}, []) | 
					
						
							|  |  |  | 						.map(pair => { | 
					
						
							|  |  |  | 							// extend combination pairs with size and integrated size
 | 
					
						
							|  |  |  | 							const a = pair[0].size(options); | 
					
						
							|  |  |  | 							const b = pair[1].size(options); | 
					
						
							|  |  |  | 							const ab = pair[0].integratedSize(pair[1], options); | 
					
						
							|  |  |  | 							return [a + b - ab, ab, pair[0], pair[1], a, b]; | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.filter(extendedPair => { | 
					
						
							|  |  |  | 							// filter pairs that do not have an integratedSize
 | 
					
						
							|  |  |  | 							// meaning they can NOT be integrated!
 | 
					
						
							|  |  |  | 							return extendedPair[1] !== false; | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.sort((a, b) => { | 
					
						
							|  |  |  | 							// sadly javascript does an inplace sort here
 | 
					
						
							|  |  |  | 							// sort them by size
 | 
					
						
							|  |  |  | 							const diff = b[0] - a[0]; | 
					
						
							|  |  |  | 							if (diff !== 0) return diff; | 
					
						
							|  |  |  | 							return a[1] - b[1]; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const pair = sortedExtendedPairCombinations[0]; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					if (pair && pair[2].integrate(pair[3], "limit")) { | 
					
						
							|  |  |  | 						chunks.splice(chunks.indexOf(pair[3]), 1); | 
					
						
							|  |  |  | 						return true; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-02-04 01:12:19 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-05 12:17:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = LimitChunkCountPlugin; |