| 
									
										
										
										
											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"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | const schema = require("../../schemas/plugins/ids/OccurrenceModuleIdsPlugin.json"); | 
					
						
							| 
									
										
										
										
											2018-09-04 15:04:05 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	compareModulesByPreOrderIndexOrIdentifier | 
					
						
							|  |  |  | } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | const { assignAscendingModuleIds } = 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 = {}) { | 
					
						
							| 
									
										
										
										
											2019-08-07 21:55:03 +08:00
										 |  |  | 		validateOptions(schema, options, { | 
					
						
							|  |  |  | 			name: "Occurrence Order Module Ids Plugin", | 
					
						
							|  |  |  | 			baseDataPath: "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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 			compilation.hooks.moduleIds.tap("OccurrenceModuleIdsPlugin", modules => { | 
					
						
							|  |  |  | 				const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				const modulesInOccurrenceOrder = Array.from(modules).filter( | 
					
						
							| 
									
										
										
										
											2019-09-26 21:51:40 +08:00
										 |  |  | 					m => | 
					
						
							|  |  |  | 						m.needId && | 
					
						
							|  |  |  | 						chunkGraph.getNumberOfModuleChunks(m) > 0 && | 
					
						
							|  |  |  | 						chunkGraph.getModuleId(m) === null | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 				 * @param {Iterable<ModuleGraphConnection>} connections connections | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				 * @returns {number} count of occurs | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 				const countOccursInEntry = connections => { | 
					
						
							|  |  |  | 					let sum = 0; | 
					
						
							|  |  |  | 					for (const c of connections) { | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 						if (!c.isActive(undefined)) continue; | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 						if (!c.originModule) continue; | 
					
						
							|  |  |  | 						sum += initialChunkChunkMap.get(c.originModule); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 				 * @param {Iterable<ModuleGraphConnection>} connections connections | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				 * @returns {number} count of occurs | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 				const countOccurs = connections => { | 
					
						
							|  |  |  | 					let sum = 0; | 
					
						
							|  |  |  | 					for (const c of connections) { | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 						if (!c.isActive(undefined)) continue; | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 						if (!c.originModule) continue; | 
					
						
							|  |  |  | 						if (!c.dependency) continue; | 
					
						
							|  |  |  | 						const factor = c.dependency.getNumberOfIdOccurrences(); | 
					
						
							|  |  |  | 						if (factor === 0) continue; | 
					
						
							|  |  |  | 						sum += factor * chunkGraph.getNumberOfModuleChunks(c.originModule); | 
					
						
							| 
									
										
										
										
											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 = | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 							countOccursInEntry(moduleGraph.getIncomingConnections(m)) + | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 							initialChunkChunkMap.get(m) + | 
					
						
							|  |  |  | 							entryCountMap.get(m); | 
					
						
							|  |  |  | 						occursInInitialChunksMap.set(m, result); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (const m of modules) { | 
					
						
							|  |  |  | 					const result = | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 						countOccurs(moduleGraph.getIncomingConnections(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); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 00:08:44 +08:00
										 |  |  | 				assignAscendingModuleIds(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; |