webpack/lib/DependencyTemplates.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-07-11 19:05:13 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2018-07-11 19:05:13 +08:00
"use strict";
const { DEFAULTS } = require("./config/defaults");
2018-07-11 19:05:13 +08:00
const createHash = require("./util/createHash");
2025-09-02 22:12:40 +08:00
/** @typedef {import("./Compilation").DependencyConstructor} DependencyConstructor */
/** @typedef {import("./DependencyTemplate")} DependencyTemplate */
/** @typedef {typeof import("./util/Hash")} Hash */
2018-07-11 19:05:13 +08:00
class DependencyTemplates {
/**
* @param {string | Hash} hashFunction the hash function to use
*/
constructor(hashFunction = DEFAULTS.HASH_FUNCTION) {
2025-03-14 00:34:04 +08:00
/** @type {Map<DependencyConstructor, DependencyTemplate>} */
2018-07-11 19:05:13 +08:00
this._map = new Map();
/** @type {string} */
this._hash = "31d6cfe0d16ae931b73c59d7e0c089c0";
this._hashFunction = hashFunction;
2018-07-11 19:05:13 +08:00
}
/**
* @param {DependencyConstructor} dependency Constructor of Dependency
2023-06-22 05:55:05 +08:00
* @returns {DependencyTemplate | undefined} template for this dependency
2018-07-11 19:05:13 +08:00
*/
get(dependency) {
return this._map.get(dependency);
}
/**
* @param {DependencyConstructor} dependency Constructor of Dependency
2018-07-11 19:05:13 +08:00
* @param {DependencyTemplate} dependencyTemplate template for this dependency
* @returns {void}
*/
set(dependency, dependencyTemplate) {
this._map.set(dependency, dependencyTemplate);
}
/**
* @param {string} part additional hash contributor
* @returns {void}
*/
updateHash(part) {
const hash = createHash(this._hashFunction);
2021-09-26 08:15:41 +08:00
hash.update(`${this._hash}${part}`);
2025-10-07 22:40:59 +08:00
this._hash = hash.digest("hex");
2018-07-11 19:05:13 +08:00
}
getHash() {
return this._hash;
}
clone() {
const newInstance = new DependencyTemplates(this._hashFunction);
2018-07-11 19:05:13 +08:00
newInstance._map = new Map(this._map);
newInstance._hash = this._hash;
return newInstance;
}
}
module.exports = DependencyTemplates;