DependencyTemplates.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 createHash = require("./util/createHash");
  8. /** @typedef {import("./Compilation").DependencyConstructor} DependencyConstructor */
  9. /** @typedef {import("./DependencyTemplate")} DependencyTemplate */
  10. /** @typedef {import("./util/Hash").HashFunction} HashFunction */
  11. class DependencyTemplates {
  12. /**
  13. * Creates an instance of DependencyTemplates.
  14. * @param {HashFunction} hashFunction the hash function to use
  15. */
  16. constructor(hashFunction = DEFAULTS.HASH_FUNCTION) {
  17. /** @type {Map<DependencyConstructor, DependencyTemplate>} */
  18. this._map = new Map();
  19. /** @type {string} */
  20. this._hash = "31d6cfe0d16ae931b73c59d7e0c089c0";
  21. /** @type {HashFunction} */
  22. this._hashFunction = hashFunction;
  23. }
  24. /**
  25. * Returns template for this dependency.
  26. * @param {DependencyConstructor} dependency Constructor of Dependency
  27. * @returns {DependencyTemplate | undefined} template for this dependency
  28. */
  29. get(dependency) {
  30. return this._map.get(dependency);
  31. }
  32. /**
  33. * Updates value using the provided dependency.
  34. * @param {DependencyConstructor} dependency Constructor of Dependency
  35. * @param {DependencyTemplate} dependencyTemplate template for this dependency
  36. * @returns {void}
  37. */
  38. set(dependency, dependencyTemplate) {
  39. this._map.set(dependency, dependencyTemplate);
  40. }
  41. /**
  42. * Updates the hash with the data contributed by this instance.
  43. * @param {string} part additional hash contributor
  44. * @returns {void}
  45. */
  46. updateHash(part) {
  47. const hash = createHash(this._hashFunction);
  48. hash.update(`${this._hash}${part}`);
  49. this._hash = hash.digest("hex");
  50. }
  51. getHash() {
  52. return this._hash;
  53. }
  54. clone() {
  55. const newInstance = new DependencyTemplates(this._hashFunction);
  56. newInstance._map = new Map(this._map);
  57. newInstance._hash = this._hash;
  58. return newInstance;
  59. }
  60. }
  61. module.exports = DependencyTemplates;