RuntimeModule.js 7.0 KB

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