RuntimeModule.js 7.7 KB

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