Generator.js 6.7 KB

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