Generator.js 5.0 KB

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