条件定义
"import"
、"require"
、"node"
、"node-addons"
和 "default"
条件在 Node.js 核心中定义和实现,如上所述。
"node-addons"
条件可用于提供使用原生 C++ 插件的入口点。
但是,可以通过 --no-addons
标志禁用此条件。
当使用 "node-addons"
时,建议将 "default"
视为提供更通用入口点的增强功能,例如使用 WebAssembly 而不是原生插件。
Node.js 不知道其他条件字符串,因此默认情况下会被忽略。 Node.js 以外的运行时或工具可以自行决定使用它们。
这些用户条件可以通过 --conditions
标志在 Node.js 中启用。
Node.js 当前支持以下条件定义:
"browser"
- 任何环境,它实现了 Web 浏览器中 JavaScript 提供的全局浏览器 API 的标准子集,包括 DOM API。"development"
- 可用于定义仅限开发的环境入口点。 必须始终与"production"
互斥。"production"
- 可用于定义生产环境入口点。 必须始终与"development"
互斥。
上述用户条件可以通过 --conditions
标志在 Node.js 中启用。
可以使用平台特定条件,例如 "deno"
、"electron"
或 "react-native"
,但虽然这些平台没有实现或集成意图,但 Node.js 并未明确认可上述条件。
可以通过向本节的 Node.js 文档创建拉取请求,将新的条件定义添加到此列表中。 在此处列出新条件定义的要求是:
- 对于所有实现者来说,定义应该是清晰明确的。
- 为什么需要条件的用例应该清楚地证明。
- 应该存在足够的现有实现用法。
- 条件名称不应与另一个条件定义或广泛使用的条件冲突。
- 条件定义的列表应该为生态系统提供协调效益,否则这是不可能的。 例如,对于特定于公司或特定于应用程序的条件,情况不一定如此。
上述定义可能会在适当的时候移到专门的条件仓库中。
The "import"
, "require"
, "node"
, "node-addons"
and "default"
conditions are defined and implemented in Node.js core,
as specified above.
The "node-addons"
condition can be used to provide an entry point which
uses native C++ addons. However, this condition can be disabled via the
--no-addons
flag. When using "node-addons"
, it's recommended to treat
"default"
as an enhancement that provides a more universal entry point, e.g.
using WebAssembly instead of a native addon.
Other condition strings are unknown to Node.js and thus ignored by default. Runtimes or tools other than Node.js can use them at their discretion.
These user conditions can be enabled in Node.js via the --conditions
flag.
The following condition definitions are currently endorsed by Node.js:
"browser"
- any environment which implements a standard subset of global browser APIs available from JavaScript in web browsers, including the DOM APIs."development"
- can be used to define a development-only environment entry point. Must always be mutually exclusive with"production"
."production"
- can be used to define a production environment entry point. Must always be mutually exclusive with"development"
.
The above user conditions can be enabled in Node.js via the --conditions
flag.
Platform specific conditions such as "deno"
, "electron"
, or "react-native"
may be used, but while there remain no implementation or integration intent
from these platforms, the above are not explicitly endorsed by Node.js.
New conditions definitions may be added to this list by creating a pull request to the Node.js documentation for this section. The requirements for listing a new condition definition here are that:
- The definition should be clear and unambiguous for all implementers.
- The use case for why the condition is needed should be clearly justified.
- There should exist sufficient existing implementation usage.
- The condition name should not conflict with another condition definition or condition in wide usage.
- The listing of the condition definition should provide a coordination benefit to the ecosystem that wouldn't otherwise be possible. For example, this would not necessarily be the case for company-specific or application-specific conditions.
The above definitions may be moved to a dedicated conditions registry in due course.