Generator.js 4.9 KB

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