Generator.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("webpack-sources").Source} Source */
  7. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  8. /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
  9. /** @typedef {import("./Compilation")} Compilation */
  10. /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
  11. /** @typedef {import("./ConcatenationScope")} ConcatenationScope */
  12. /** @typedef {import("./DependencyTemplate")} DependencyTemplate */
  13. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  14. /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  15. /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
  16. /** @typedef {import("./Module").SourceTypes} SourceTypes */
  17. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  18. /** @typedef {import("./NormalModule")} NormalModule */
  19. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  20. /** @typedef {import("./util/Hash")} Hash */
  21. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  22. /**
  23. * @template T
  24. * @typedef {import("./InitFragment")<T>} InitFragment
  25. */
  26. /** @typedef {Map<"url", { [key: string]: string }> & Map<"fullContentHash", string> & Map<"contentHash", string> & Map<"filename", string> & Map<"assetInfo", AssetInfo> & Map<"chunkInitFragments", InitFragment<GenerateContext>[]>} KnownGenerateContextData */
  27. /** @typedef {KnownGenerateContextData & Record<string, EXPECTED_ANY>} GenerateContextData */
  28. /**
  29. * @typedef {object} GenerateContext
  30. * @property {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
  31. * @property {RuntimeTemplate} runtimeTemplate the runtime template
  32. * @property {ModuleGraph} moduleGraph the module graph
  33. * @property {ChunkGraph} chunkGraph the chunk graph
  34. * @property {RuntimeRequirements} runtimeRequirements the requirements for runtime
  35. * @property {RuntimeSpec} runtime the runtime
  36. * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
  37. * @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
  38. * @property {string} type which kind of code should be generated
  39. * @property {() => GenerateContextData=} getData get access to the code generation data
  40. */
  41. /**
  42. * @callback GenerateErrorFn
  43. * @param {Error} error the error
  44. * @param {NormalModule} module module for which the code should be generated
  45. * @param {GenerateContext} generateContext context for generate
  46. * @returns {Source | null} generated code
  47. */
  48. /**
  49. * @typedef {object} UpdateHashContext
  50. * @property {NormalModule} module the module
  51. * @property {ChunkGraph} chunkGraph
  52. * @property {RuntimeSpec} runtime
  53. * @property {RuntimeTemplate=} runtimeTemplate
  54. */
  55. class Generator {
  56. /**
  57. * @param {Record<string, Generator>} map map of types
  58. * @returns {ByTypeGenerator} generator by type
  59. */
  60. static byType(map) {
  61. return new ByTypeGenerator(map);
  62. }
  63. /* istanbul ignore next */
  64. /**
  65. * @abstract
  66. * @param {NormalModule} module fresh module
  67. * @returns {SourceTypes} available types (do not mutate)
  68. */
  69. getTypes(module) {
  70. const AbstractMethodError = require("./AbstractMethodError");
  71. throw new AbstractMethodError();
  72. }
  73. /* istanbul ignore next */
  74. /**
  75. * @abstract
  76. * @param {NormalModule} module the module
  77. * @param {string=} type source type
  78. * @returns {number} estimate size of the module
  79. */
  80. getSize(module, type) {
  81. const AbstractMethodError = require("./AbstractMethodError");
  82. throw new AbstractMethodError();
  83. }
  84. /* istanbul ignore next */
  85. /**
  86. * @abstract
  87. * @param {NormalModule} module module for which the code should be generated
  88. * @param {GenerateContext} generateContext context for generate
  89. * @returns {Source | null} generated code
  90. */
  91. generate(
  92. module,
  93. { dependencyTemplates, runtimeTemplate, moduleGraph, type }
  94. ) {
  95. const AbstractMethodError = require("./AbstractMethodError");
  96. throw new AbstractMethodError();
  97. }
  98. /**
  99. * @param {NormalModule} module module for which the bailout reason should be determined
  100. * @param {ConcatenationBailoutReasonContext} context context
  101. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  102. */
  103. getConcatenationBailoutReason(module, context) {
  104. return `Module Concatenation is not implemented for ${this.constructor.name}`;
  105. }
  106. /**
  107. * @param {Hash} hash hash that will be modified
  108. * @param {UpdateHashContext} updateHashContext context for updating hash
  109. */
  110. updateHash(hash, { module, runtime }) {
  111. // no nothing
  112. }
  113. }
  114. /**
  115. * @this {ByTypeGenerator}
  116. * @type {GenerateErrorFn}
  117. */
  118. function generateError(error, module, generateContext) {
  119. const type = generateContext.type;
  120. const generator =
  121. /** @type {Generator & { generateError?: GenerateErrorFn }} */
  122. (this.map[type]);
  123. if (!generator) {
  124. throw new Error(`Generator.byType: no generator specified for ${type}`);
  125. }
  126. if (typeof generator.generateError === "undefined") {
  127. return null;
  128. }
  129. return generator.generateError(error, module, generateContext);
  130. }
  131. class ByTypeGenerator extends Generator {
  132. /**
  133. * @param {Record<string, Generator>} map map of types
  134. */
  135. constructor(map) {
  136. super();
  137. this.map = map;
  138. this._types = new Set(Object.keys(map));
  139. /** @type {GenerateErrorFn | undefined} */
  140. this.generateError = generateError.bind(this);
  141. }
  142. /**
  143. * @param {NormalModule} module fresh module
  144. * @returns {SourceTypes} available types (do not mutate)
  145. */
  146. getTypes(module) {
  147. return this._types;
  148. }
  149. /**
  150. * @param {NormalModule} module the module
  151. * @param {string=} type source type
  152. * @returns {number} estimate size of the module
  153. */
  154. getSize(module, type = "javascript") {
  155. const t = type;
  156. const generator = this.map[t];
  157. return generator ? generator.getSize(module, t) : 0;
  158. }
  159. /**
  160. * @param {NormalModule} module module for which the code should be generated
  161. * @param {GenerateContext} generateContext context for generate
  162. * @returns {Source | null} generated code
  163. */
  164. generate(module, generateContext) {
  165. const type = generateContext.type;
  166. const generator = this.map[type];
  167. if (!generator) {
  168. throw new Error(`Generator.byType: no generator specified for ${type}`);
  169. }
  170. return generator.generate(module, generateContext);
  171. }
  172. }
  173. module.exports = Generator;