time-analytics-webpack-plugin
Version:
analytize the time of loaders and plugins
52 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = require("crypto");
const const_1 = require("../const");
const utils_1 = require("../utils");
/**
* The object which will be used as the key of WeakMap for compilation/compiler.
*
* Need this, because WeakMap only accepts key as object.
*/
class WebpackWeakMapId {
constructor() {
this.id = (0, crypto_1.randomUUID)();
}
}
(0, utils_1.assert)(WeakMap, 'WeakMap must be existed in current runtime.');
const originSet = WeakMap.prototype.set;
const originGet = WeakMap.prototype.get;
/**
* Whether one object is `Compiler` in webpack
*/
function isCompiler(obj) {
return obj?.constructor?.name === 'Compiler';
}
/**
* Whether one object is `Compilation` in webpack
*/
function isCompilation(obj) {
return obj?.constructor?.name === 'Compilation';
}
function GetOrAddHackKeyFor(key) {
let finalKey = key;
// The reference of Proxy and its target is different
// Then `NormalModule.getCompilationHooks(compilation).loader` might get wrong hook
// Hack the WeakMap, so that we could generate a unique ID for each to generate a unique
if (isCompiler(key) || isCompilation(key)) {
if (!key[const_1.WEBPACK_WEAK_MAP_ID_KEY]) {
key[const_1.WEBPACK_WEAK_MAP_ID_KEY] = new WebpackWeakMapId();
}
finalKey = key[const_1.WEBPACK_WEAK_MAP_ID_KEY];
}
return finalKey;
}
WeakMap.prototype.set = function setHack(key, value) {
const finalKey = GetOrAddHackKeyFor(key);
return originSet.call(this, finalKey, value);
};
WeakMap.prototype.get = function setHack(key) {
const finalKey = GetOrAddHackKeyFor(key);
return originGet.call(this, finalKey);
};
//# sourceMappingURL=hackWeakMap.js.map