CodeGenerationResults.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { DEFAULTS } = require("./config/defaults");
  7. const { getOrInsert } = require("./util/MapHelpers");
  8. const { first } = require("./util/SetHelpers");
  9. const createHash = require("./util/createHash");
  10. const { RuntimeSpecMap, runtimeToString } = require("./util/runtime");
  11. /** @typedef {import("webpack-sources").Source} Source */
  12. /** @typedef {import("./Module")} Module */
  13. /** @typedef {import("./Module").SourceType} SourceType */
  14. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  15. /** @typedef {import("./Module").CodeGenerationResultData} CodeGenerationResultData */
  16. /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
  17. /** @typedef {typeof import("./util/Hash")} Hash */
  18. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  19. class CodeGenerationResults {
  20. /**
  21. * @param {string | Hash} hashFunction the hash function to use
  22. */
  23. constructor(hashFunction = DEFAULTS.HASH_FUNCTION) {
  24. /** @type {Map<Module, RuntimeSpecMap<CodeGenerationResult>>} */
  25. this.map = new Map();
  26. this._hashFunction = hashFunction;
  27. }
  28. /**
  29. * @param {Module} module the module
  30. * @param {RuntimeSpec} runtime runtime(s)
  31. * @returns {CodeGenerationResult} the CodeGenerationResult
  32. */
  33. get(module, runtime) {
  34. const entry = this.map.get(module);
  35. if (entry === undefined) {
  36. throw new Error(
  37. `No code generation entry for ${module.identifier()} (existing entries: ${Array.from(
  38. this.map.keys(),
  39. (m) => m.identifier()
  40. ).join(", ")})`
  41. );
  42. }
  43. if (runtime === undefined) {
  44. if (entry.size > 1) {
  45. const results = new Set(entry.values());
  46. if (results.size !== 1) {
  47. throw new Error(
  48. `No unique code generation entry for unspecified runtime for ${module.identifier()} (existing runtimes: ${Array.from(
  49. entry.keys(),
  50. (r) => runtimeToString(r)
  51. ).join(", ")}).
  52. Caller might not support runtime-dependent code generation (opt-out via optimization.usedExports: "global").`
  53. );
  54. }
  55. return /** @type {CodeGenerationResult} */ (first(results));
  56. }
  57. return /** @type {CodeGenerationResult} */ (entry.values().next().value);
  58. }
  59. const result = entry.get(runtime);
  60. if (result === undefined) {
  61. throw new Error(
  62. `No code generation entry for runtime ${runtimeToString(
  63. runtime
  64. )} for ${module.identifier()} (existing runtimes: ${Array.from(
  65. entry.keys(),
  66. (r) => runtimeToString(r)
  67. ).join(", ")})`
  68. );
  69. }
  70. return result;
  71. }
  72. /**
  73. * @param {Module} module the module
  74. * @param {RuntimeSpec} runtime runtime(s)
  75. * @returns {boolean} true, when we have data for this
  76. */
  77. has(module, runtime) {
  78. const entry = this.map.get(module);
  79. if (entry === undefined) {
  80. return false;
  81. }
  82. if (runtime !== undefined) {
  83. return entry.has(runtime);
  84. } else if (entry.size > 1) {
  85. const results = new Set(entry.values());
  86. return results.size === 1;
  87. }
  88. return entry.size === 1;
  89. }
  90. /**
  91. * @param {Module} module the module
  92. * @param {RuntimeSpec} runtime runtime(s)
  93. * @param {SourceType} sourceType the source type
  94. * @returns {Source} a source
  95. */
  96. getSource(module, runtime, sourceType) {
  97. return /** @type {Source} */ (
  98. this.get(module, runtime).sources.get(sourceType)
  99. );
  100. }
  101. /**
  102. * @param {Module} module the module
  103. * @param {RuntimeSpec} runtime runtime(s)
  104. * @returns {ReadOnlyRuntimeRequirements | null} runtime requirements
  105. */
  106. getRuntimeRequirements(module, runtime) {
  107. return this.get(module, runtime).runtimeRequirements;
  108. }
  109. /**
  110. * @param {Module} module the module
  111. * @param {RuntimeSpec} runtime runtime(s)
  112. * @param {string} key data key
  113. * @returns {ReturnType<CodeGenerationResultData["get"]>} data generated by code generation
  114. */
  115. getData(module, runtime, key) {
  116. const data = this.get(module, runtime).data;
  117. return data === undefined ? undefined : data.get(key);
  118. }
  119. /**
  120. * @param {Module} module the module
  121. * @param {RuntimeSpec} runtime runtime(s)
  122. * @returns {string} hash of the code generation
  123. */
  124. getHash(module, runtime) {
  125. const info = this.get(module, runtime);
  126. if (info.hash !== undefined) return info.hash;
  127. const hash = createHash(this._hashFunction);
  128. for (const [type, source] of info.sources) {
  129. hash.update(type);
  130. source.updateHash(hash);
  131. }
  132. if (info.runtimeRequirements) {
  133. for (const rr of info.runtimeRequirements) hash.update(rr);
  134. }
  135. return (info.hash = hash.digest("hex"));
  136. }
  137. /**
  138. * @param {Module} module the module
  139. * @param {RuntimeSpec} runtime runtime(s)
  140. * @param {CodeGenerationResult} result result from module
  141. * @returns {void}
  142. */
  143. add(module, runtime, result) {
  144. const map = getOrInsert(this.map, module, () => new RuntimeSpecMap());
  145. map.set(runtime, result);
  146. }
  147. }
  148. module.exports = CodeGenerationResults;