| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-05 13:42:36 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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/MinChunkSizePlugin.json"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 13:42:36 +08:00
										 |  |  | class MinChunkSizePlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | 		validateOptions(schema, options, "Min Chunk Size Plugin"); | 
					
						
							| 
									
										
										
										
											2017-01-05 13:42:36 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2015-05-28 00:46:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 13:42:36 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		const options = this.options; | 
					
						
							|  |  |  | 		const minChunkSize = options.minChunkSize; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("MinChunkSizePlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.optimizeChunksAdvanced.tap( | 
					
						
							|  |  |  | 				"MinChunkSizePlugin", | 
					
						
							|  |  |  | 				chunks => { | 
					
						
							|  |  |  | 					const equalOptions = { | 
					
						
							|  |  |  | 						chunkOverhead: 1, | 
					
						
							|  |  |  | 						entryChunkMultiplicator: 1 | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const sortedSizeFilteredExtendedPairCombinations = 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; | 
					
						
							|  |  |  | 						}, []) | 
					
						
							|  |  |  | 						.filter(pair => { | 
					
						
							|  |  |  | 							// check if one of the chunks sizes is smaller than the minChunkSize
 | 
					
						
							|  |  |  | 							const p0SmallerThanMinChunkSize = | 
					
						
							|  |  |  | 								pair[0].size(equalOptions) < minChunkSize; | 
					
						
							|  |  |  | 							const p1SmallerThanMinChunkSize = | 
					
						
							|  |  |  | 								pair[1].size(equalOptions) < minChunkSize; | 
					
						
							|  |  |  | 							return p0SmallerThanMinChunkSize || p1SmallerThanMinChunkSize; | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.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]]; | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.filter(pair => { | 
					
						
							|  |  |  | 							// filter pairs that do not have an integratedSize
 | 
					
						
							|  |  |  | 							// meaning they can NOT be integrated!
 | 
					
						
							|  |  |  | 							return pair[1] !== false; | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.sort((a, b) => { | 
					
						
							|  |  |  | 							// sadly javascript does an inplace sort here
 | 
					
						
							|  |  |  | 							// sort by size
 | 
					
						
							|  |  |  | 							const diff = b[0] - a[0]; | 
					
						
							|  |  |  | 							if (diff !== 0) return diff; | 
					
						
							|  |  |  | 							return a[1] - b[1]; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					if (sortedSizeFilteredExtendedPairCombinations.length === 0) return; | 
					
						
							| 
									
										
										
										
											2017-02-05 09:19:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const pair = sortedSizeFilteredExtendedPairCombinations[0]; | 
					
						
							| 
									
										
										
										
											2013-06-18 00:55:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					pair[2].integrate(pair[3], "min-size"); | 
					
						
							|  |  |  | 					chunks.splice(chunks.indexOf(pair[3]), 1); | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-02-11 17:52:19 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-05 13:42:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = MinChunkSizePlugin; |