| 
									
										
										
										
											2014-02-05 19:05:09 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-02-05 19:05:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 04:30:27 +08:00
										 |  |  | const { STAGE_ADVANCED } = require("../OptimizationStages"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 01:52:25 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} AggressiveMergingPluginOptions | 
					
						
							|  |  |  |  * @property {number=} minSizeReduce minimal size reduction to trigger merging | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | class AggressiveMergingPlugin { | 
					
						
							| 
									
										
										
										
											2023-06-04 01:52:25 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {AggressiveMergingPluginOptions=} [options] options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if ( | 
					
						
							|  |  |  | 			(options !== undefined && typeof options !== "object") || | 
					
						
							|  |  |  | 			Array.isArray(options) | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							|  |  |  | 				"Argument should be an options object. To use defaults, pass in nothing.\nFor more info on options, see https://webpack.js.org/plugins/" | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		this.options = options || {}; | 
					
						
							| 
									
										
										
										
											2014-02-05 19:05:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:33:54 +08:00
										 |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 		const minSizeReduce = options.minSizeReduce || 1.5; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"AggressiveMergingPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2018-07-31 04:30:27 +08:00
										 |  |  | 				compilation.hooks.optimizeChunks.tap( | 
					
						
							| 
									
										
										
										
											2018-12-09 19:54:17 +08:00
										 |  |  | 					{ | 
					
						
							| 
									
										
										
										
											2018-07-31 04:30:27 +08:00
										 |  |  | 						name: "AggressiveMergingPlugin", | 
					
						
							|  |  |  | 						stage: STAGE_ADVANCED | 
					
						
							| 
									
										
										
										
											2018-12-09 19:54:17 +08:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					chunks => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 						const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 						/** @type {{a: Chunk, b: Chunk, improvement: number}[]} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						let combinations = []; | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  | 						for (const a of chunks) { | 
					
						
							|  |  |  | 							if (a.canBeInitial()) continue; | 
					
						
							|  |  |  | 							for (const b of chunks) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								if (b.canBeInitial()) continue; | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  | 								if (b === a) break; | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 								if (!chunkGraph.canChunksBeIntegrated(a, b)) { | 
					
						
							|  |  |  | 									continue; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								const aSize = chunkGraph.getChunkSize(b, { | 
					
						
							|  |  |  | 									chunkOverhead: 0 | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 								const bSize = chunkGraph.getChunkSize(a, { | 
					
						
							|  |  |  | 									chunkOverhead: 0 | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 								const abSize = chunkGraph.getIntegratedChunksSize(b, a, { | 
					
						
							|  |  |  | 									chunkOverhead: 0 | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 								const improvement = (aSize + bSize) / abSize; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								combinations.push({ | 
					
						
							|  |  |  | 									a, | 
					
						
							|  |  |  | 									b, | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 									improvement | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								}); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						combinations.sort((a, b) => { | 
					
						
							|  |  |  | 							return b.improvement - a.improvement; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const pair = combinations[0]; | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (!pair) return; | 
					
						
							|  |  |  | 						if (pair.improvement < minSizeReduce) return; | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 						chunkGraph.integrateChunks(pair.b, pair.a); | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  | 						compilation.chunks.delete(pair.a); | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 						return true; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-05 05:18:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = AggressiveMergingPlugin; |