Generator.js 5.9 KB

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