嵌套的条件


稳定性: 1 - 实验

除了直接映射,Node.js 还支持嵌套条件对象。

例如,要定义一个包,它只有双模式入口点用于 Node.js 而不是浏览器:

{
  "main": "./main.js",
  "exports": {
    "node": {
      "import": "./feature-node.mjs",
      "require": "./feature-node.cjs"
    },
    "default": "./feature.mjs",
  }
}

条件继续按顺序与平面条件匹配。 如果嵌套条件没有任何映射,则将继续检查父条件的剩余条件。 通过这种方式,嵌套条件的行为类似于嵌套的 JavaScript if 语句。

Stability: 1 - Experimental

In addition to direct mappings, Node.js also supports nested condition objects.

For example, to define a package that only has dual mode entry points for use in Node.js but not the browser:

{
  "main": "./main.js",
  "exports": {
    "node": {
      "import": "./feature-node.mjs",
      "require": "./feature-node.cjs"
    },
    "default": "./feature.mjs",
  }
}

Conditions continue to be matched in order as with flat conditions. If a nested conditional does not have any mapping it will continue checking the remaining conditions of the parent condition. In this way nested conditions behave analogously to nested JavaScript if statements.