Generator.js 5.0 KB

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