| 
									
										
										
										
											2015-11-30 03:16:01 +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-02 01:26:22 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | const createHash = require("../util/createHash"); | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	getUsedModuleIdsAndModules, | 
					
						
							|  |  |  | 	getFullModuleName | 
					
						
							|  |  |  | } = require("./IdHelpers"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */ | 
					
						
							| 
									
										
										
										
											2023-05-27 01:21:35 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							|  |  |  | 	require("../../schemas/plugins/HashedModuleIdsPlugin.check.js"), | 
					
						
							|  |  |  | 	() => require("../../schemas/plugins/HashedModuleIdsPlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Hashed Module Ids Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | class HashedModuleIdsPlugin { | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {HashedModuleIdsPluginOptions=} options options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(options = {}) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | 		/** @type {HashedModuleIdsPluginOptions} */ | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 		this.options = { | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 			context: undefined, | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 			hashFunction: "md4", | 
					
						
							|  |  |  | 			hashDigest: "base64", | 
					
						
							|  |  |  | 			hashDigestLength: 4, | 
					
						
							|  |  |  | 			...options | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 01:21:35 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("HashedModuleIdsPlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | 			compilation.hooks.moduleIds.tap("HashedModuleIdsPlugin", () => { | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  | 				const chunkGraph = compilation.chunkGraph; | 
					
						
							| 
									
										
										
										
											2018-12-07 23:07:40 +08:00
										 |  |  | 				const context = this.options.context | 
					
						
							|  |  |  | 					? this.options.context | 
					
						
							|  |  |  | 					: compiler.context; | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 01:14:50 +08:00
										 |  |  | 				const [usedIds, modules] = getUsedModuleIdsAndModules(compilation); | 
					
						
							|  |  |  | 				const modulesInNaturalOrder = modules.sort( | 
					
						
							|  |  |  | 					compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph) | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  | 				for (const module of modulesInNaturalOrder) { | 
					
						
							| 
									
										
										
										
											2018-12-08 01:12:04 +08:00
										 |  |  | 					const ident = getFullModuleName(module, context, compiler.root); | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 					const hash = createHash( | 
					
						
							|  |  |  | 						/** @type {NonNullable<HashedModuleIdsPluginOptions["hashFunction"]>} */ ( | 
					
						
							|  |  |  | 							options.hashFunction | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 					hash.update(ident || ""); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 					const hashId = /** @type {string} */ ( | 
					
						
							|  |  |  | 						hash.digest(options.hashDigest) | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 					let len = options.hashDigestLength; | 
					
						
							| 
									
										
										
										
											2023-05-27 01:21:35 +08:00
										 |  |  | 					while (usedIds.has(hashId.slice(0, len))) | 
					
						
							|  |  |  | 						/** @type {number} */ (len)++; | 
					
						
							| 
									
										
										
										
											2022-03-14 05:54:18 +08:00
										 |  |  | 					const moduleId = hashId.slice(0, len); | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 					chunkGraph.setModuleId(module, moduleId); | 
					
						
							|  |  |  | 					usedIds.add(moduleId); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-30 03:16:01 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-30 03:16:01 +08:00
										 |  |  | module.exports = HashedModuleIdsPlugin; |