RuntimeModule.js 5.9 KB

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