RuntimeModule.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const OriginalSource = require("webpack-sources").OriginalSource;
  8. const Module = require("./Module");
  9. const {
  10. JAVASCRIPT_TYPES,
  11. RUNTIME_TYPES
  12. } = require("./ModuleSourceTypeConstants");
  13. const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
  14. /**
  15. * @import {
  16. * WebpackOptionsNormalizedWithDefaults as WebpackOptions
  17. * } from "./config/defaults"
  18. */
  19. /** @import Chunk from "./Chunk" */
  20. /** @import ChunkGraph from "./ChunkGraph" */
  21. /** @import Compilation from "./Compilation" */
  22. /** @import { UpdateHashContext } from "./Dependency" */
  23. /**
  24. * @import {
  25. * BuildMeta,
  26. * BuildInfo,
  27. * BuildCallback,
  28. * CodeGenerationContext,
  29. * CodeGenerationResult,
  30. * NeedBuildCallback,
  31. * NeedBuildContext,
  32. * Sources,
  33. * BasicSourceTypes,
  34. * SourceTypes
  35. * } from "./Module"
  36. */
  37. /** @import RequestShortener from "./RequestShortener" */
  38. /** @import { ResolverWithOptions } from "./ResolverFactory" */
  39. /** @import Hash from "./util/Hash" */
  40. /** @import { InputFileSystem } from "./util/fs" */
  41. class RuntimeModule extends Module {
  42. /**
  43. * Creates an instance of RuntimeModule.
  44. * @param {string} name a readable name
  45. * @param {number=} stage an optional stage
  46. */
  47. constructor(name, stage = 0) {
  48. super(WEBPACK_MODULE_TYPE_RUNTIME);
  49. /** @type {string} */
  50. this.name = name;
  51. /** @type {number} */
  52. this.stage = stage;
  53. /** @type {BuildMeta} */
  54. this.buildMeta = {};
  55. /** @type {BuildInfo} */
  56. this.buildInfo = {};
  57. /** @type {Compilation | undefined} */
  58. this.compilation = undefined;
  59. /** @type {Chunk | undefined} */
  60. this.chunk = undefined;
  61. /** @type {ChunkGraph | undefined} */
  62. this.chunkGraph = undefined;
  63. /** @type {boolean} */
  64. this.fullHash = false;
  65. /** @type {boolean} */
  66. this.dependentHash = false;
  67. /** @type {string | undefined | null} */
  68. this._cachedGeneratedCode = undefined;
  69. }
  70. /**
  71. * Processes the provided compilation.
  72. * @param {Compilation} compilation the compilation
  73. * @param {Chunk} chunk the chunk
  74. * @param {ChunkGraph} chunkGraph the chunk graph
  75. * @returns {void}
  76. */
  77. attach(compilation, chunk, chunkGraph = compilation.chunkGraph) {
  78. this.compilation = compilation;
  79. this.chunk = chunk;
  80. this.chunkGraph = chunkGraph;
  81. }
  82. /**
  83. * Returns the unique identifier used to reference this module.
  84. * @returns {string} a unique identifier of the module
  85. */
  86. identifier() {
  87. return `webpack/runtime/${this.name}`;
  88. }
  89. /**
  90. * Returns a human-readable identifier for this module.
  91. * @param {RequestShortener} requestShortener the request shortener
  92. * @returns {string} a user readable identifier of the module
  93. */
  94. readableIdentifier(requestShortener) {
  95. return `webpack/runtime/${this.name}`;
  96. }
  97. /**
  98. * Checks whether the module needs to be rebuilt for the current build state.
  99. * @param {NeedBuildContext} context context info
  100. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  101. * @returns {void}
  102. */
  103. needBuild(context, callback) {
  104. return callback(null, false);
  105. }
  106. /**
  107. * Builds the module using the provided compilation context.
  108. * @param {WebpackOptions} options webpack options
  109. * @param {Compilation} compilation the compilation
  110. * @param {ResolverWithOptions} resolver the resolver
  111. * @param {InputFileSystem} fs the file system
  112. * @param {BuildCallback} callback callback function
  113. * @returns {void}
  114. */
  115. build(options, compilation, resolver, fs, callback) {
  116. // do nothing
  117. // should not be called as runtime modules are added later to the compilation
  118. callback();
  119. }
  120. /**
  121. * Updates the hash with the data contributed by this instance.
  122. * @param {Hash} hash the hash used to track dependencies
  123. * @param {UpdateHashContext} context context
  124. * @returns {void}
  125. */
  126. updateHash(hash, context) {
  127. hash.update(this.name);
  128. hash.update(`${this.stage}`);
  129. try {
  130. const code =
  131. this.fullHash || this.dependentHash
  132. ? // Do not use getGeneratedCode here, because i. e. compilation hash might be not
  133. // ready at this point. We will cache it later instead.
  134. this.generate()
  135. : this.getGeneratedCode();
  136. if (code !== null && code !== undefined) {
  137. hash.update(code);
  138. }
  139. } catch (err) {
  140. hash.update(/** @type {Error} */ (err).message);
  141. }
  142. super.updateHash(hash, context);
  143. }
  144. /**
  145. * Returns the source types this module can generate.
  146. * @returns {SourceTypes} types available (do not mutate)
  147. */
  148. getSourceTypes() {
  149. return RUNTIME_TYPES;
  150. }
  151. /**
  152. * Basic source types are high-level categories like javascript, css, webassembly, etc.
  153. * We only have built-in knowledge about the javascript basic type here; other basic types may be
  154. * added or changed over time by generators and do not need to be handled or detected here.
  155. *
  156. * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
  157. * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
  158. * @returns {BasicSourceTypes} types available (do not mutate)
  159. */
  160. getSourceBasicTypes() {
  161. return JAVASCRIPT_TYPES;
  162. }
  163. /**
  164. * Generates code and runtime requirements for this module.
  165. * @param {CodeGenerationContext} context context for code generation
  166. * @returns {CodeGenerationResult} result
  167. */
  168. codeGeneration(context) {
  169. /** @type {Sources} */
  170. const sources = new Map();
  171. const generatedCode = this.getGeneratedCode();
  172. if (generatedCode) {
  173. sources.set(
  174. WEBPACK_MODULE_TYPE_RUNTIME,
  175. this.useSourceMap || this.useSimpleSourceMap
  176. ? new OriginalSource(generatedCode, this.identifier())
  177. : new RawSource(generatedCode)
  178. );
  179. }
  180. return {
  181. sources,
  182. runtimeRequirements: null
  183. };
  184. }
  185. /**
  186. * Returns the estimated size for the requested source type.
  187. * @param {string=} type the source type for which the size should be estimated
  188. * @returns {number} the estimated size of the module (must be non-zero)
  189. */
  190. size(type) {
  191. try {
  192. const source = this.getGeneratedCode();
  193. return source ? source.length : 0;
  194. } catch (_err) {
  195. return 0;
  196. }
  197. }
  198. /* istanbul ignore next */
  199. /**
  200. * Generates runtime code for this runtime module.
  201. * @abstract
  202. * @returns {string | null} runtime code
  203. */
  204. generate() {
  205. const AbstractMethodError = require("./errors/AbstractMethodError");
  206. throw new AbstractMethodError();
  207. }
  208. /**
  209. * Gets generated code.
  210. * @returns {string | null} runtime code
  211. */
  212. getGeneratedCode() {
  213. if (this._cachedGeneratedCode) {
  214. return this._cachedGeneratedCode;
  215. }
  216. return (this._cachedGeneratedCode = this.generate());
  217. }
  218. /**
  219. * Returns true, if the runtime module should get it's own scope.
  220. * When false, `generate()` must emit complete statements ending with `;`
  221. * so a following runtime IIFE is not parsed as a call (ASI).
  222. * @returns {boolean} true, if the runtime module should get it's own scope
  223. */
  224. shouldIsolate() {
  225. return true;
  226. }
  227. }
  228. /**
  229. * Runtime modules without any dependencies to other runtime modules
  230. */
  231. RuntimeModule.STAGE_NORMAL = 0;
  232. /**
  233. * Runtime modules with simple dependencies on other runtime modules
  234. */
  235. RuntimeModule.STAGE_BASIC = 5;
  236. /**
  237. * Runtime modules which attach to handlers of other runtime modules
  238. */
  239. RuntimeModule.STAGE_ATTACH = 10;
  240. /**
  241. * Runtime modules which trigger actions on bootstrap
  242. */
  243. RuntimeModule.STAGE_TRIGGER = 20;
  244. module.exports = RuntimeModule;