| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const OriginalSource = require("webpack-sources").OriginalSource; | 
					
						
							| 
									
										
										
										
											2019-09-28 01:50:23 +08:00
										 |  |  | const AbstractMethodError = require("./AbstractMethodError"); | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | const Module = require("./Module"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2020-02-17 17:27:46 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | /** @typedef {import("./Chunk")} Chunk */ | 
					
						
							|  |  |  | /** @typedef {import("./ChunkGraph")} ChunkGraph */ | 
					
						
							|  |  |  | /** @typedef {import("./Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */ | 
					
						
							|  |  |  | /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */ | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */ | 
					
						
							|  |  |  | /** @typedef {import("./RequestShortener")} RequestShortener */ | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */ | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | /** @typedef {import("./WebpackError")} WebpackError */ | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | /** @typedef {import("./util/Hash")} Hash */ | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | const TYPES = new Set(["runtime"]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | class RuntimeModule extends Module { | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} name a readable name | 
					
						
							|  |  |  | 	 * @param {number=} stage an optional stage | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	constructor(name, stage = 0) { | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 		super("runtime"); | 
					
						
							|  |  |  | 		this.name = name; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		this.stage = stage; | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 		this.buildMeta = {}; | 
					
						
							|  |  |  | 		this.buildInfo = {}; | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 		/** @type {Compilation} */ | 
					
						
							|  |  |  | 		this.compilation = undefined; | 
					
						
							|  |  |  | 		/** @type {Chunk} */ | 
					
						
							|  |  |  | 		this.chunk = undefined; | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 		/** @type {string} */ | 
					
						
							| 
									
										
										
										
											2018-11-20 19:05:12 +08:00
										 |  |  | 		this._cachedGeneratedCode = undefined; | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation | 
					
						
							|  |  |  | 	 * @param {Chunk} chunk the chunk | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	attach(compilation, chunk) { | 
					
						
							|  |  |  | 		this.compilation = compilation; | 
					
						
							|  |  |  | 		this.chunk = chunk; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} a unique identifier of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	identifier() { | 
					
						
							| 
									
										
										
										
											2018-11-23 17:02:47 +08:00
										 |  |  | 		return `webpack/runtime/${this.name}`; | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {RequestShortener} requestShortener the request shortener | 
					
						
							|  |  |  | 	 * @returns {string} a user readable identifier of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	readableIdentifier(requestShortener) { | 
					
						
							| 
									
										
										
										
											2018-11-23 17:02:47 +08:00
										 |  |  | 		return `webpack/runtime/${this.name}`; | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NeedBuildContext} context context info | 
					
						
							|  |  |  | 	 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	needBuild(context, callback) { | 
					
						
							|  |  |  | 		return callback(null, false); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 	 * @param {WebpackOptions} options webpack options | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	 * @param {Compilation} compilation the compilation | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 	 * @param {ResolverWithOptions} resolver the resolver | 
					
						
							|  |  |  | 	 * @param {InputFileSystem} fs the file system | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	 * @param {function(WebpackError=): void} callback callback function | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	build(options, compilation, resolver, fs, callback) { | 
					
						
							|  |  |  | 		// do nothing
 | 
					
						
							|  |  |  | 		// should not be called as runtime modules are added later to the compilation
 | 
					
						
							|  |  |  | 		callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Hash} hash the hash used to track dependencies | 
					
						
							|  |  |  | 	 * @param {ChunkGraph} chunkGraph the chunk graph | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateHash(hash, chunkGraph) { | 
					
						
							| 
									
										
										
										
											2018-11-20 19:05:12 +08:00
										 |  |  | 		// Do not use getGeneratedCode here, because i. e. compilation hash is not
 | 
					
						
							|  |  |  | 		// ready at this point. We will cache it later instead.
 | 
					
						
							| 
									
										
										
										
											2019-09-26 17:46:43 +08:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			hash.update(this.generate()); | 
					
						
							|  |  |  | 		} catch (err) { | 
					
						
							|  |  |  | 			hash.update(err.message); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 		super.updateHash(hash, chunkGraph); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {Set<string>} types availiable (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSourceTypes() { | 
					
						
							|  |  |  | 		return TYPES; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 	 * @param {CodeGenerationContext} context context for code generation | 
					
						
							|  |  |  | 	 * @returns {CodeGenerationResult} result | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 	codeGeneration(context) { | 
					
						
							|  |  |  | 		const sources = new Map(); | 
					
						
							| 
									
										
										
										
											2019-09-26 21:51:40 +08:00
										 |  |  | 		const generatedCode = this.getGeneratedCode(); | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 		if (generatedCode) { | 
					
						
							|  |  |  | 			sources.set( | 
					
						
							|  |  |  | 				"runtime", | 
					
						
							|  |  |  | 				new OriginalSource(generatedCode, this.identifier()) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			sources, | 
					
						
							|  |  |  | 			runtimeRequirements: null | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	 * @param {string=} type the source type for which the size should be estimated | 
					
						
							| 
									
										
										
										
											2019-05-13 18:29:29 +08:00
										 |  |  | 	 * @returns {number} the estimated size of the module (must be non-zero) | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	size(type) { | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 		if (this._cachedGeneratedCode) { | 
					
						
							|  |  |  | 			return this._cachedGeneratedCode.length; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @abstract | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-11-20 19:05:12 +08:00
										 |  |  | 	generate() { | 
					
						
							| 
									
										
										
										
											2019-09-28 01:50:23 +08:00
										 |  |  | 		throw new AbstractMethodError(); | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 19:05:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getGeneratedCode() { | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 		if (this._cachedGeneratedCode) { | 
					
						
							|  |  |  | 			return this._cachedGeneratedCode; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-20 19:05:12 +08:00
										 |  |  | 		return (this._cachedGeneratedCode = this.generate()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = RuntimeModule; |