RuntimeModule.js 5.6 KB

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