Generator.js 5.1 KB

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