CodeGenerationResults.js 4.6 KB

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