| 
									
										
										
										
											2015-06-28 04:47:51 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-12-29 15:05:11 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:45:55 +08:00
										 |  |  | const createHash = require("./util/createHash"); | 
					
						
							|  |  |  | const RequestShortener = require("./RequestShortener"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getHash = str => { | 
					
						
							|  |  |  | 	const hash = createHash("md4"); | 
					
						
							|  |  |  | 	hash.update(str); | 
					
						
							|  |  |  | 	return hash.digest("hex").substr(0, 4); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 15:05:11 +08:00
										 |  |  | class NamedModulesPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options || {}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("NamedModulesPlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.beforeModuleIds.tap("NamedModulesPlugin", modules => { | 
					
						
							| 
									
										
										
										
											2018-05-01 03:45:55 +08:00
										 |  |  | 				const namedModules = new Map(); | 
					
						
							| 
									
										
										
										
											2018-05-01 19:26:12 +08:00
										 |  |  | 				const context = this.options.context || compiler.options.context; | 
					
						
							| 
									
										
										
										
											2018-04-30 01:29:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				for (const module of modules) { | 
					
						
							|  |  |  | 					if (module.id === null && module.libIdent) { | 
					
						
							| 
									
										
										
										
											2018-05-01 19:26:12 +08:00
										 |  |  | 						module.id = module.libIdent({ context }); | 
					
						
							| 
									
										
										
										
											2016-12-29 15:05:11 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-04-30 01:29:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:45:55 +08:00
										 |  |  | 					if (module.id !== null) { | 
					
						
							| 
									
										
										
										
											2018-05-01 19:26:12 +08:00
										 |  |  | 						const namedModule = namedModules.get(module.id); | 
					
						
							|  |  |  | 						if (namedModule !== undefined) { | 
					
						
							|  |  |  | 							namedModule.push(module); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:45:55 +08:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							namedModules.set(module.id, [module]); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-04-30 01:29:38 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-04-30 01:29:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 19:26:12 +08:00
										 |  |  | 				for (const namedModule of namedModules.values()) { | 
					
						
							| 
									
										
										
										
											2018-04-30 01:29:38 +08:00
										 |  |  | 					if (namedModule.length > 1) { | 
					
						
							| 
									
										
										
										
											2018-05-01 19:26:12 +08:00
										 |  |  | 						for (const module of namedModule) { | 
					
						
							|  |  |  | 							const requestShortener = new RequestShortener(context); | 
					
						
							|  |  |  | 							module.id = `${module.id}?${getHash( | 
					
						
							|  |  |  | 								requestShortener.shorten(module.identifier()) | 
					
						
							|  |  |  | 							)}`;
 | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-04-30 01:29:38 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-05-01 19:26:12 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-12-29 15:05:11 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-28 04:47:51 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-29 15:05:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-28 04:47:51 +08:00
										 |  |  | module.exports = NamedModulesPlugin; |