双 CommonJS/ES 模块包
【Dual CommonJS/ES module packages】
在 Node.js 引入对 ES 模块支持之前,包作者通常会在他们的包中同时包含 CommonJS 和 ES 模块的 JavaScript 源文件,并在 package.json 中通过 "main" 指定 CommonJS 入口点,通过 package.json 中的 "module" 指定 ES 模块入口点。这使得 Node.js 可以运行 CommonJS 入口点,而构建工具(如打包器)则使用 ES 模块入口点,因为 Node.js 会忽略(并且仍然忽略)顶层的 "module" 字段。
【Prior to the introduction of support for ES modules in Node.js, it was a common
pattern for package authors to include both CommonJS and ES module JavaScript
sources in their package, with package.json "main" specifying the
CommonJS entry point and package.json "module" specifying the ES module
entry point.
This enabled Node.js to run the CommonJS entry point while build tools such as
bundlers used the ES module entry point, since Node.js ignored (and still
ignores) the top-level "module" field.】
Node.js现在可以运行ES模块的入口点,一个包可以同时包含两者 CommonJS 和 ES 模块的入口点(通过不同的指定符,如 “pkg”和“pkg/es-module”,或通过条件导出在同一指定符下两者。与“模”仅供打包者使用的情景不同, 或者在评估前,ES模块文件会在被 Node.js,ES模块入口点引用的文件被评估为ES 模块。
【Node.js can now run ES module entry points, and a package can contain both
CommonJS and ES module entry points (either via separate specifiers such as
'pkg' and 'pkg/es-module', or both at the same specifier via Conditional
exports). Unlike in the scenario where "module" is only used by bundlers,
or ES module files are transpiled into CommonJS on the fly before evaluation by
Node.js, the files referenced by the ES module entry point are evaluated as ES
modules.】