RuntimeModule.js 6.1 KB

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