Generator.js 4.7 KB

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