| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 15:10:25 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 14:27:44 +08:00
										 |  |  | const { 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 */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | /** @typedef {import("./Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | /** @typedef {import("./ChunkGraph")} ChunkGraph */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */ | 
					
						
							| 
									
										
										
										
											2018-06-20 18:04:47 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | /** @typedef {import("./ModuleGraph")} ModuleGraph */ | 
					
						
							|  |  |  | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							| 
									
										
										
										
											2018-11-02 22:39:51 +08:00
										 |  |  | /** @typedef {import("./util/createHash").Hash} Hash */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} RenderContext | 
					
						
							|  |  |  |  * @property {Chunk} chunk the chunk | 
					
						
							|  |  |  |  * @property {DependencyTemplates} dependencyTemplates the dependency templates | 
					
						
							|  |  |  |  * @property {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							|  |  |  |  * @property {ModuleGraph} moduleGraph the module graph | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  |  * @property {ChunkGraph} chunkGraph the chunk graph | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-06-20 18:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 14:27:44 +08:00
										 |  |  | module.exports = class ModuleTemplate { | 
					
						
							| 
									
										
										
										
											2018-11-07 21:03:25 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							|  |  |  | 	 * @param {string} type the module template type | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	constructor(runtimeTemplate, type) { | 
					
						
							| 
									
										
										
										
											2017-12-07 17:31:00 +08:00
										 |  |  | 		this.runtimeTemplate = runtimeTemplate; | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 		this.type = type; | 
					
						
							| 
									
										
										
										
											2018-07-30 20:25:40 +08:00
										 |  |  | 		this.hooks = Object.freeze({ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 			/** @type {SyncWaterfallHook<Source, Module, RenderContext>} */ | 
					
						
							|  |  |  | 			content: new SyncWaterfallHook(["source", "module", "context"]), | 
					
						
							|  |  |  | 			/** @type {SyncWaterfallHook<Source, Module, RenderContext>} */ | 
					
						
							|  |  |  | 			module: new SyncWaterfallHook(["source", "module", "context"]), | 
					
						
							|  |  |  | 			/** @type {SyncWaterfallHook<Source, Module, RenderContext>} */ | 
					
						
							|  |  |  | 			render: new SyncWaterfallHook(["source", "module", "context"]), | 
					
						
							|  |  |  | 			/** @type {SyncWaterfallHook<Source, Module, RenderContext>} */ | 
					
						
							|  |  |  | 			package: new SyncWaterfallHook(["source", "module", "context"]), | 
					
						
							| 
									
										
										
										
											2018-11-02 22:39:51 +08:00
										 |  |  | 			/** @type {SyncHook<Hash>} */ | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 			hash: new SyncHook(["hash"]) | 
					
						
							| 
									
										
										
										
											2018-07-30 20:25:40 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	 * @param {RenderContext} ctx render ctx | 
					
						
							| 
									
										
										
										
											2018-06-20 18:04:47 +08:00
										 |  |  | 	 * @returns {Source} the source | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	render(module, ctx) { | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 			const { | 
					
						
							|  |  |  | 				runtimeTemplate, | 
					
						
							|  |  |  | 				dependencyTemplates, | 
					
						
							|  |  |  | 				moduleGraph, | 
					
						
							|  |  |  | 				chunkGraph | 
					
						
							|  |  |  | 			} = ctx; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 			const moduleSource = module.source({ | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 				dependencyTemplates, | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 				runtimeTemplate, | 
					
						
							|  |  |  | 				moduleGraph, | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 				chunkGraph, | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 				type: this.type | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 			const moduleSourcePostContent = this.hooks.content.call( | 
					
						
							|  |  |  | 				moduleSource, | 
					
						
							|  |  |  | 				module, | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 				ctx | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			const moduleSourcePostModule = this.hooks.module.call( | 
					
						
							|  |  |  | 				moduleSourcePostContent, | 
					
						
							|  |  |  | 				module, | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 				ctx | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			const moduleSourcePostRender = this.hooks.render.call( | 
					
						
							|  |  |  | 				moduleSourcePostModule, | 
					
						
							|  |  |  | 				module, | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 				ctx | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 			return this.hooks.package.call(moduleSourcePostRender, module, ctx); | 
					
						
							| 
									
										
										
										
											2018-04-28 17:09:31 +08:00
										 |  |  | 		} 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-02 22:39:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Updates hash with information from this template | 
					
						
							|  |  |  | 	 * @param {Hash} hash the hash to update | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }; |