| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2018-08-23 02:17:49 +08:00
										 |  |  | /** @typedef {import("./ChunkGraph")} ChunkGraph */ | 
					
						
							| 
									
										
										
										
											2021-12-01 21:15:19 +08:00
										 |  |  | /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */ | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | /** @typedef {import("./Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | /** @typedef {import("./ConcatenationScope")} ConcatenationScope */ | 
					
						
							| 
									
										
										
										
											2018-07-23 23:33:29 +08:00
										 |  |  | /** @typedef {import("./DependencyTemplate")} DependencyTemplate */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */ | 
					
						
							| 
									
										
										
										
											2020-09-15 16:07:04 +08:00
										 |  |  | /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | /** @typedef {import("./ModuleGraph")} ModuleGraph */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | /** @typedef {import("./NormalModule")} NormalModule */ | 
					
						
							|  |  |  | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | /** @typedef {import("./util/Hash")} Hash */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */ | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} GenerateContext | 
					
						
							|  |  |  |  * @property {DependencyTemplates} dependencyTemplates mapping from dependencies to templates | 
					
						
							|  |  |  |  * @property {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  |  * @property {ModuleGraph} moduleGraph the module graph | 
					
						
							| 
									
										
										
										
											2018-08-23 02:17:49 +08:00
										 |  |  |  * @property {ChunkGraph} chunkGraph the chunk graph | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  |  * @property {Set<string>} runtimeRequirements the requirements for runtime | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  |  * @property {RuntimeSpec} runtime the runtime | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  |  * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  |  * @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that) | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  |  * @property {string} type which kind of code should be generated | 
					
						
							| 
									
										
										
										
											2020-10-11 07:17:03 +08:00
										 |  |  |  * @property {function(): Map<string, any>=} getData get access to the code generation data | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} UpdateHashContext | 
					
						
							|  |  |  |  * @property {NormalModule} module the module | 
					
						
							| 
									
										
										
										
											2020-07-28 01:36:06 +08:00
										 |  |  |  * @property {ChunkGraph} chunkGraph | 
					
						
							|  |  |  |  * @property {RuntimeSpec} runtime | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Generator { | 
					
						
							|  |  |  | 	static byType(map) { | 
					
						
							|  |  |  | 		return new ByTypeGenerator(map); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-16 15:37:11 +08:00
										 |  |  | 	/* istanbul ignore next */ | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @abstract | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	 * @param {NormalModule} module fresh module | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	getTypes(module) { | 
					
						
							| 
									
										
										
										
											2020-04-16 15:37:11 +08:00
										 |  |  | 		const AbstractMethodError = require("./AbstractMethodError"); | 
					
						
							| 
									
										
										
										
											2019-09-28 01:50:23 +08:00
										 |  |  | 		throw new AbstractMethodError(); | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-16 15:37:11 +08:00
										 |  |  | 	/* istanbul ignore next */ | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @abstract | 
					
						
							|  |  |  | 	 * @param {NormalModule} module the module | 
					
						
							|  |  |  | 	 * @param {string=} type source type | 
					
						
							|  |  |  | 	 * @returns {number} estimate size of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSize(module, type) { | 
					
						
							| 
									
										
										
										
											2020-04-16 15:37:11 +08:00
										 |  |  | 		const AbstractMethodError = require("./AbstractMethodError"); | 
					
						
							| 
									
										
										
										
											2019-09-28 01:50:23 +08:00
										 |  |  | 		throw new AbstractMethodError(); | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-16 15:37:11 +08:00
										 |  |  | 	/* istanbul ignore next */ | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @abstract | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	 * @returns {Source} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	generate( | 
					
						
							|  |  |  | 		module, | 
					
						
							|  |  |  | 		{ dependencyTemplates, runtimeTemplate, moduleGraph, type } | 
					
						
							|  |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2020-04-16 15:37:11 +08:00
										 |  |  | 		const AbstractMethodError = require("./AbstractMethodError"); | 
					
						
							| 
									
										
										
										
											2019-09-28 01:50:23 +08:00
										 |  |  | 		throw new AbstractMethodError(); | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 04:37:57 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module for which the bailout reason should be determined | 
					
						
							| 
									
										
										
										
											2020-09-15 16:07:04 +08:00
										 |  |  | 	 * @param {ConcatenationBailoutReasonContext} context context | 
					
						
							| 
									
										
										
										
											2020-09-12 04:37:57 +08:00
										 |  |  | 	 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-09-15 16:07:04 +08:00
										 |  |  | 	getConcatenationBailoutReason(module, context) { | 
					
						
							| 
									
										
										
										
											2020-09-12 04:37:57 +08:00
										 |  |  | 		return `Module Concatenation is not implemented for ${this.constructor.name}`; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Hash} hash hash that will be modified | 
					
						
							|  |  |  | 	 * @param {UpdateHashContext} updateHashContext context for updating hash | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-07-28 01:36:06 +08:00
										 |  |  | 	updateHash(hash, { module, runtime }) { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		// no nothing
 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ByTypeGenerator extends Generator { | 
					
						
							|  |  |  | 	constructor(map) { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 		this.map = map; | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 		this._types = new Set(Object.keys(map)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	 * @param {NormalModule} module fresh module | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	getTypes(module) { | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 		return this._types; | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module the module | 
					
						
							|  |  |  | 	 * @param {string=} type source type | 
					
						
							|  |  |  | 	 * @returns {number} estimate size of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSize(module, type) { | 
					
						
							|  |  |  | 		const t = type || "javascript"; | 
					
						
							|  |  |  | 		const generator = this.map[t]; | 
					
						
							|  |  |  | 		return generator ? generator.getSize(module, t) : 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							| 
									
										
										
										
											2018-07-21 00:17:51 +08:00
										 |  |  | 	 * @returns {Source} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 	generate(module, generateContext) { | 
					
						
							|  |  |  | 		const type = generateContext.type; | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 		const generator = this.map[type]; | 
					
						
							|  |  |  | 		if (!generator) { | 
					
						
							|  |  |  | 			throw new Error(`Generator.byType: no generator specified for ${type}`); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 		return generator.generate(module, generateContext); | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Generator; |