示例:已修补的依赖
【Example: Patched dependency】
重定向的依赖可以根据应用的需要提供减弱或修改后的功能。例如,通过封装原始函数来记录函数持续时间的日志数据:
【Redirected dependencies can provide attenuated or modified functionality as fits the application. For example, log data about timing of function durations by wrapping the original:】
const original = require('fn');
module.exports = function fn(...args) {
console.time();
try {
return new.target ?
Reflect.construct(original, args) :
Reflect.apply(original, this, args);
} finally {
console.timeEnd();
}
};