RuntimeModule.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
  10. /** @typedef {import("webpack-sources").Source} Source */
  11. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  12. /** @typedef {import("./Chunk")} Chunk */
  13. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  14. /** @typedef {import("./Compilation")} Compilation */
  15. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  16. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  17. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  18. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  19. /** @typedef {import("./RequestShortener")} RequestShortener */
  20. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  21. /** @typedef {import("./WebpackError")} WebpackError */
  22. /** @typedef {import("./util/Hash")} Hash */
  23. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  24. const TYPES = new Set([WEBPACK_MODULE_TYPE_RUNTIME]);
  25. class RuntimeModule extends Module {
  26. /**
  27. * @param {string} name a readable name
  28. * @param {number=} stage an optional stage
  29. */
  30. constructor(name, stage = 0) {
  31. super(WEBPACK_MODULE_TYPE_RUNTIME);
  32. this.name = name;
  33. this.stage = stage;
  34. this.buildMeta = {};
  35. this.buildInfo = {};
  36. /** @type {Compilation | undefined} */
  37. this.compilation = undefined;
  38. /** @type {Chunk | undefined} */
  39. this.chunk = undefined;
  40. /** @type {ChunkGraph | undefined} */
  41. this.chunkGraph = undefined;
  42. this.fullHash = false;
  43. this.dependentHash = false;
  44. /** @type {string | undefined} */
  45. this._cachedGeneratedCode = undefined;
  46. }
  47. /**
  48. * @param {Compilation} compilation the compilation
  49. * @param {Chunk} chunk the chunk
  50. * @param {ChunkGraph} chunkGraph the chunk graph
  51. * @returns {void}
  52. */
  53. attach(compilation, chunk, chunkGraph = compilation.chunkGraph) {
  54. this.compilation = compilation;
  55. this.chunk = chunk;
  56. this.chunkGraph = chunkGraph;
  57. }
  58. /**
  59. * @returns {string} a unique identifier of the module
  60. */
  61. identifier() {
  62. return `webpack/runtime/${this.name}`;
  63. }
  64. /**
  65. * @param {RequestShortener} requestShortener the request shortener
  66. * @returns {string} a user readable identifier of the module
  67. */
  68. readableIdentifier(requestShortener) {
  69. return `webpack/runtime/${this.name}`;
  70. }
  71. /**
  72. * @param {NeedBuildContext} context context info
  73. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  74. * @returns {void}
  75. */
  76. needBuild(context, callback) {
  77. return callback(null, false);
  78. }
  79. /**
  80. * @param {WebpackOptions} options webpack options
  81. * @param {Compilation} compilation the compilation
  82. * @param {ResolverWithOptions} resolver the resolver
  83. * @param {InputFileSystem} fs the file system
  84. * @param {function(WebpackError=): void} callback callback function
  85. * @returns {void}
  86. */
  87. build(options, compilation, resolver, fs, callback) {
  88. // do nothing
  89. // should not be called as runtime modules are added later to the compilation
  90. callback();
  91. }
  92. /**
  93. * @param {Hash} hash the hash used to track dependencies
  94. * @param {UpdateHashContext} context context
  95. * @returns {void}
  96. */
  97. updateHash(hash, context) {
  98. hash.update(this.name);
  99. hash.update(`${this.stage}`);
  100. try {
  101. if (this.fullHash || this.dependentHash) {
  102. // Do not use getGeneratedCode here, because i. e. compilation hash might be not
  103. // ready at this point. We will cache it later instead.
  104. hash.update(this.generate());
  105. } else {
  106. hash.update(this.getGeneratedCode());
  107. }
  108. } catch (err) {
  109. hash.update(/** @type {Error} */ (err).message);
  110. }
  111. super.updateHash(hash, context);
  112. }
  113. /**
  114. * @returns {Set<string>} types available (do not mutate)
  115. */
  116. getSourceTypes() {
  117. return TYPES;
  118. }
  119. /**
  120. * @param {CodeGenerationContext} context context for code generation
  121. * @returns {CodeGenerationResult} result
  122. */
  123. codeGeneration(context) {
  124. const sources = new Map();
  125. const generatedCode = this.getGeneratedCode();
  126. if (generatedCode) {
  127. sources.set(
  128. WEBPACK_MODULE_TYPE_RUNTIME,
  129. this.useSourceMap || this.useSimpleSourceMap
  130. ? new OriginalSource(generatedCode, this.identifier())
  131. : new RawSource(generatedCode)
  132. );
  133. }
  134. return {
  135. sources,
  136. runtimeRequirements: null
  137. };
  138. }
  139. /**
  140. * @param {string=} type the source type for which the size should be estimated
  141. * @returns {number} the estimated size of the module (must be non-zero)
  142. */
  143. size(type) {
  144. try {
  145. const source = this.getGeneratedCode();
  146. return source ? source.length : 0;
  147. } catch (e) {
  148. return 0;
  149. }
  150. }
  151. /* istanbul ignore next */
  152. /**
  153. * @abstract
  154. * @returns {string | null} runtime code
  155. */
  156. generate() {
  157. const AbstractMethodError = require("./AbstractMethodError");
  158. throw new AbstractMethodError();
  159. }
  160. /**
  161. * @returns {string | null} runtime code
  162. */
  163. getGeneratedCode() {
  164. if (this._cachedGeneratedCode) {
  165. return /** @type {string | null} */ (this._cachedGeneratedCode);
  166. }
  167. return (this._cachedGeneratedCode = this.generate());
  168. }
  169. /**
  170. * @returns {boolean} true, if the runtime module should get it's own scope
  171. */
  172. shouldIsolate() {
  173. return true;
  174. }
  175. }
  176. /**
  177. * Runtime modules without any dependencies to other runtime modules
  178. */
  179. RuntimeModule.STAGE_NORMAL = 0;
  180. /**
  181. * Runtime modules with simple dependencies on other runtime modules
  182. */
  183. RuntimeModule.STAGE_BASIC = 5;
  184. /**
  185. * Runtime modules which attach to handlers of other runtime modules
  186. */
  187. RuntimeModule.STAGE_ATTACH = 10;
  188. /**
  189. * Runtime modules which trigger actions on bootstrap
  190. */
  191. RuntimeModule.STAGE_TRIGGER = 20;
  192. module.exports = RuntimeModule;