模块如何导出到 Winston 包中?

How is the module exported in Winston package?

我正在尝试阅读 winston 代码库,但我在理解这个主模块的导出方式时遇到了问题?我从未见过这种模式,有人可以向我解释它是如何工作的吗?不是应该 const exports = winston 而不是相反吗?

这就是我要说的:

/**
* Setup to expose.
* @type {Object}
*/
const winston = exports;

exports 只是一个对象。 winston 是对 exports 对象的引用。

和写exports.version = ...等是一样的

您可以在其余代码中看到作者正在向此 winston 对象添加函数,如下所示:winston.Container = require('./winston/container'); winston.loggers = new winston.Container();

因此,作者没有使用 module.exports 的常用语法来导出模块,而是实际上在导出对象本身上工作。

所以基本上 'winston' 只是对导出的引用,它是 module.exports 的 shorthand。

所以你可以认为整个模块是这样写的: module.exports.somePropertyName = 'someThing';