Generator.js 4.7 KB

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