| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 15:04:05 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	compareModulesByPreOrderIndexOrIdentifier | 
					
						
							|  |  |  | } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("../util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	assignAscendingModuleIds, | 
					
						
							|  |  |  | 	getUsedModuleIdsAndModules | 
					
						
							|  |  |  | } = require("./IdHelpers"); | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/ids/OccurrenceModuleIdsPlugin").OccurrenceModuleIdsPluginOptions} OccurrenceModuleIdsPluginOptions */ | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							| 
									
										
										
										
											2018-07-24 21:30:37 +08:00
										 |  |  | /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							|  |  |  | 	require("../../schemas/plugins/ids/OccurrenceModuleIdsPlugin.check.js"), | 
					
						
							|  |  |  | 	() => require("../../schemas/plugins/ids/OccurrenceModuleIdsPlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Occurrence Order Module Ids Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | class OccurrenceModuleIdsPlugin { | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {OccurrenceModuleIdsPluginOptions=} options options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 	constructor(options = {}) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		const prioritiseInitial = this.options.prioritiseInitial; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("OccurrenceModuleIdsPlugin", compilation => { | 
					
						
							|  |  |  | 			const moduleGraph = compilation.moduleGraph; | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | 			compilation.hooks.moduleIds.tap("OccurrenceModuleIdsPlugin", () => { | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | 				const [usedIds, modulesInOccurrenceOrder] = | 
					
						
							|  |  |  | 					getUsedModuleIdsAndModules(compilation); | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				const occursInInitialChunksMap = new Map(); | 
					
						
							|  |  |  | 				const occursInAllChunksMap = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				const initialChunkChunkMap = new Map(); | 
					
						
							|  |  |  | 				const entryCountMap = new Map(); | 
					
						
							|  |  |  | 				for (const m of modulesInOccurrenceOrder) { | 
					
						
							|  |  |  | 					let initial = 0; | 
					
						
							|  |  |  | 					let entry = 0; | 
					
						
							|  |  |  | 					for (const c of chunkGraph.getModuleChunksIterable(m)) { | 
					
						
							|  |  |  | 						if (c.canBeInitial()) initial++; | 
					
						
							|  |  |  | 						if (chunkGraph.isEntryModuleInChunk(m, c)) entry++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					initialChunkChunkMap.set(m, initial); | 
					
						
							|  |  |  | 					entryCountMap.set(m, entry); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 				 * @param {Module} module module | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				 * @returns {number} count of occurs | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 				const countOccursInEntry = module => { | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 					let sum = 0; | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 					for (const [ | 
					
						
							|  |  |  | 						originModule, | 
					
						
							|  |  |  | 						connections | 
					
						
							|  |  |  | 					] of moduleGraph.getIncomingConnectionsByOriginModule(module)) { | 
					
						
							|  |  |  | 						if (!originModule) continue; | 
					
						
							|  |  |  | 						if (!connections.some(c => c.isTargetActive(undefined))) continue; | 
					
						
							| 
									
										
										
										
											2023-04-07 14:59:19 +08:00
										 |  |  | 						sum += initialChunkChunkMap.get(originModule) || 0; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 					return sum; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 				 * @param {Module} module module | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				 * @returns {number} count of occurs | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 				const countOccurs = module => { | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 					let sum = 0; | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 					for (const [ | 
					
						
							|  |  |  | 						originModule, | 
					
						
							|  |  |  | 						connections | 
					
						
							|  |  |  | 					] of moduleGraph.getIncomingConnectionsByOriginModule(module)) { | 
					
						
							|  |  |  | 						if (!originModule) continue; | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						const chunkModules = | 
					
						
							|  |  |  | 							chunkGraph.getNumberOfModuleChunks(originModule); | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 						for (const c of connections) { | 
					
						
							|  |  |  | 							if (!c.isTargetActive(undefined)) continue; | 
					
						
							|  |  |  | 							if (!c.dependency) continue; | 
					
						
							|  |  |  | 							const factor = c.dependency.getNumberOfIdOccurrences(); | 
					
						
							|  |  |  | 							if (factor === 0) continue; | 
					
						
							|  |  |  | 							sum += factor * chunkModules; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 					return sum; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (prioritiseInitial) { | 
					
						
							|  |  |  | 					for (const m of modulesInOccurrenceOrder) { | 
					
						
							|  |  |  | 						const result = | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 							countOccursInEntry(m) + | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 							initialChunkChunkMap.get(m) + | 
					
						
							|  |  |  | 							entryCountMap.get(m); | 
					
						
							|  |  |  | 						occursInInitialChunksMap.set(m, result); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | 				for (const m of modulesInOccurrenceOrder) { | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 					const result = | 
					
						
							| 
									
										
										
										
											2021-02-23 19:03:26 +08:00
										 |  |  | 						countOccurs(m) + | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 						chunkGraph.getNumberOfModuleChunks(m) + | 
					
						
							|  |  |  | 						entryCountMap.get(m); | 
					
						
							|  |  |  | 					occursInAllChunksMap.set(m, result); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				const naturalCompare = compareModulesByPreOrderIndexOrIdentifier( | 
					
						
							|  |  |  | 					compilation.moduleGraph | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				modulesInOccurrenceOrder.sort((a, b) => { | 
					
						
							|  |  |  | 					if (prioritiseInitial) { | 
					
						
							|  |  |  | 						const aEntryOccurs = occursInInitialChunksMap.get(a); | 
					
						
							|  |  |  | 						const bEntryOccurs = occursInInitialChunksMap.get(b); | 
					
						
							|  |  |  | 						if (aEntryOccurs > bEntryOccurs) return -1; | 
					
						
							|  |  |  | 						if (aEntryOccurs < bEntryOccurs) return 1; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					const aOccurs = occursInAllChunksMap.get(a); | 
					
						
							|  |  |  | 					const bOccurs = occursInAllChunksMap.get(b); | 
					
						
							|  |  |  | 					if (aOccurs > bOccurs) return -1; | 
					
						
							|  |  |  | 					if (aOccurs < bOccurs) return 1; | 
					
						
							|  |  |  | 					return naturalCompare(a, b); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | 				assignAscendingModuleIds( | 
					
						
							|  |  |  | 					usedIds, | 
					
						
							|  |  |  | 					modulesInOccurrenceOrder, | 
					
						
							|  |  |  | 					compilation | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-07-09 20:31:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | module.exports = OccurrenceModuleIdsPlugin; |