提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.7 KiB

  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 {typeof import("./util/Hash")} Hash */
  11. class DependencyTemplates {
  12. /**
  13. * @param {string | Hash} 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. this._hashFunction = hashFunction;
  21. }
  22. /**
  23. * @param {DependencyConstructor} dependency Constructor of Dependency
  24. * @returns {DependencyTemplate | undefined} template for this dependency
  25. */
  26. get(dependency) {
  27. return this._map.get(dependency);
  28. }
  29. /**
  30. * @param {DependencyConstructor} dependency Constructor of Dependency
  31. * @param {DependencyTemplate} dependencyTemplate template for this dependency
  32. * @returns {void}
  33. */
  34. set(dependency, dependencyTemplate) {
  35. this._map.set(dependency, dependencyTemplate);
  36. }
  37. /**
  38. * @param {string} part additional hash contributor
  39. * @returns {void}
  40. */
  41. updateHash(part) {
  42. const hash = createHash(this._hashFunction);
  43. hash.update(`${this._hash}${part}`);
  44. this._hash = hash.digest("hex");
  45. }
  46. getHash() {
  47. return this._hash;
  48. }
  49. clone() {
  50. const newInstance = new DependencyTemplates(this._hashFunction);
  51. newInstance._map = new Map(this._map);
  52. newInstance._hash = this._hash;
  53. return newInstance;
  54. }
  55. }
  56. module.exports = DependencyTemplates;