DependencyTemplates.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * @param {HashFunction} hashFunction the hash function to use
  14. */
  15. constructor(hashFunction = DEFAULTS.HASH_FUNCTION) {
  16. /** @type {Map<DependencyConstructor, DependencyTemplate>} */
  17. this._map = new Map();
  18. /** @type {string} */
  19. this._hash = "31d6cfe0d16ae931b73c59d7e0c089c0";
  20. /** @type {HashFunction} */
  21. this._hashFunction = hashFunction;
  22. }
  23. /**
  24. * @param {DependencyConstructor} dependency Constructor of Dependency
  25. * @returns {DependencyTemplate | undefined} template for this dependency
  26. */
  27. get(dependency) {
  28. return this._map.get(dependency);
  29. }
  30. /**
  31. * @param {DependencyConstructor} dependency Constructor of Dependency
  32. * @param {DependencyTemplate} dependencyTemplate template for this dependency
  33. * @returns {void}
  34. */
  35. set(dependency, dependencyTemplate) {
  36. this._map.set(dependency, dependencyTemplate);
  37. }
  38. /**
  39. * @param {string} part additional hash contributor
  40. * @returns {void}
  41. */
  42. updateHash(part) {
  43. const hash = createHash(this._hashFunction);
  44. hash.update(`${this._hash}${part}`);
  45. this._hash = hash.digest("hex");
  46. }
  47. getHash() {
  48. return this._hash;
  49. }
  50. clone() {
  51. const newInstance = new DependencyTemplates(this._hashFunction);
  52. newInstance._map = new Map(this._map);
  53. newInstance._hash = this._hash;
  54. return newInstance;
  55. }
  56. }
  57. module.exports = DependencyTemplates;