| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | /** @typedef {import("./NormalModule")} NormalModule */ | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | /** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate */ | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Generator { | 
					
						
							|  |  |  | 	static byType(map) { | 
					
						
							|  |  |  | 		return new ByTypeGenerator(map); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @abstract | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							|  |  |  | 	 * @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	 * @param {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							|  |  |  | 	 * @param {string} type which kind of code should be generated | 
					
						
							|  |  |  | 	 * @returns {Source} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	generate(module, dependencyTemplates, runtimeTemplate, type) { | 
					
						
							| 
									
										
										
										
											2018-07-10 04:48:12 +08:00
										 |  |  | 		throw new Error("Generator.generate: must be overridden"); | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ByTypeGenerator extends Generator { | 
					
						
							|  |  |  | 	constructor(map) { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 		this.map = map; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							|  |  |  | 	 * @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates | 
					
						
							|  |  |  | 	 * @param {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							|  |  |  | 	 * @param {string} type which kind of code should be generated | 
					
						
							|  |  |  | 	 * @returns {Source} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	generate(module, dependencyTemplates, runtimeTemplate, type) { | 
					
						
							|  |  |  | 		const generator = this.map[type]; | 
					
						
							|  |  |  | 		if (!generator) { | 
					
						
							|  |  |  | 			throw new Error(`Generator.byType: no generator specified for ${type}`); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return generator.generate( | 
					
						
							|  |  |  | 			module, | 
					
						
							|  |  |  | 			dependencyTemplates, | 
					
						
							|  |  |  | 			runtimeTemplate, | 
					
						
							|  |  |  | 			type | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Generator; |