Generator.js 6.0 KB

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