| 
									
										
										
										
											2015-11-30 03:16:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2017-11-23 17:59:29 +08:00
										 |  |  | const createHash = require("./util/createHash"); | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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/HashedModuleIdsPlugin.json"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | class HashedModuleIdsPlugin { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {HashedModuleIdsPluginOptions=} options options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		if (!options) options = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		validateOptions(schema, options, "Hashed Module Ids Plugin"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		/** @type {HashedModuleIdsPluginOptions} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.options = Object.assign( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				context: null, | 
					
						
							|  |  |  | 				hashFunction: "md4", | 
					
						
							|  |  |  | 				hashDigest: "base64", | 
					
						
							|  |  |  | 				hashDigestLength: 4 | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			options | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 			const usedIds = new Set(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			compilation.hooks.beforeModuleIds.tap( | 
					
						
							|  |  |  | 				"HashedModuleIdsPlugin", | 
					
						
							|  |  |  | 				modules => { | 
					
						
							|  |  |  | 					for (const module of modules) { | 
					
						
							|  |  |  | 						if (module.id === null && module.libIdent) { | 
					
						
							|  |  |  | 							const id = module.libIdent({ | 
					
						
							|  |  |  | 								context: this.options.context || compiler.options.context | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 							const hash = createHash(options.hashFunction); | 
					
						
							|  |  |  | 							hash.update(id); | 
					
						
							| 
									
										
										
										
											2019-07-10 17:42:34 +08:00
										 |  |  | 							const hashId = /** @type {string} */ (hash.digest( | 
					
						
							|  |  |  | 								options.hashDigest | 
					
						
							|  |  |  | 							)); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							let len = options.hashDigestLength; | 
					
						
							|  |  |  | 							while (usedIds.has(hashId.substr(0, len))) len++; | 
					
						
							|  |  |  | 							module.id = hashId.substr(0, len); | 
					
						
							|  |  |  | 							usedIds.add(module.id); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-01-02 01:26:22 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +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; |