| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-12-29 15:10:25 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable"); | 
					
						
							| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 18:04:47 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							|  |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | module.exports = class ModuleTemplate extends Tapable { | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	constructor(runtimeTemplate, type) { | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 		super(); | 
					
						
							| 
									
										
										
										
											2017-12-07 17:31:00 +08:00
										 |  |  | 		this.runtimeTemplate = runtimeTemplate; | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 		this.type = type; | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 		this.hooks = { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			content: new SyncWaterfallHook([ | 
					
						
							|  |  |  | 				"source", | 
					
						
							|  |  |  | 				"module", | 
					
						
							|  |  |  | 				"options", | 
					
						
							|  |  |  | 				"dependencyTemplates" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			module: new SyncWaterfallHook([ | 
					
						
							|  |  |  | 				"source", | 
					
						
							|  |  |  | 				"module", | 
					
						
							|  |  |  | 				"options", | 
					
						
							|  |  |  | 				"dependencyTemplates" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			render: new SyncWaterfallHook([ | 
					
						
							|  |  |  | 				"source", | 
					
						
							|  |  |  | 				"module", | 
					
						
							|  |  |  | 				"options", | 
					
						
							|  |  |  | 				"dependencyTemplates" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			package: new SyncWaterfallHook([ | 
					
						
							|  |  |  | 				"source", | 
					
						
							|  |  |  | 				"module", | 
					
						
							|  |  |  | 				"options", | 
					
						
							|  |  |  | 				"dependencyTemplates" | 
					
						
							|  |  |  | 			]), | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 			hash: new SyncHook(["hash"]) | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2016-12-29 15:10:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 18:04:47 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Module} module the module | 
					
						
							|  |  |  | 	 * @param {TODO} dependencyTemplates templates for dependencies | 
					
						
							|  |  |  | 	 * @param {TODO} options render options | 
					
						
							|  |  |  | 	 * @returns {Source} the source | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-10 18:01:51 +08:00
										 |  |  | 	render(module, dependencyTemplates, options) { | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			const moduleSource = module.source( | 
					
						
							|  |  |  | 				dependencyTemplates, | 
					
						
							|  |  |  | 				this.runtimeTemplate, | 
					
						
							|  |  |  | 				this.type | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const moduleSourcePostContent = this.hooks.content.call( | 
					
						
							|  |  |  | 				moduleSource, | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				options, | 
					
						
							|  |  |  | 				dependencyTemplates | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const moduleSourcePostModule = this.hooks.module.call( | 
					
						
							|  |  |  | 				moduleSourcePostContent, | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				options, | 
					
						
							|  |  |  | 				dependencyTemplates | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const moduleSourcePostRender = this.hooks.render.call( | 
					
						
							|  |  |  | 				moduleSourcePostModule, | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				options, | 
					
						
							|  |  |  | 				dependencyTemplates | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			return this.hooks.package.call( | 
					
						
							|  |  |  | 				moduleSourcePostRender, | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				options, | 
					
						
							|  |  |  | 				dependencyTemplates | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} catch (e) { | 
					
						
							|  |  |  | 			e.message = `${module.identifier()}\n${e.message}`; | 
					
						
							|  |  |  | 			throw e; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-29 15:10:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 15:10:25 +08:00
										 |  |  | 	updateHash(hash) { | 
					
						
							|  |  |  | 		hash.update("1"); | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 		this.hooks.hash.call(hash); | 
					
						
							| 
									
										
										
										
											2016-12-29 15:10:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | }; |