子路径的导入


稳定性: 1 - 实验

除了 "exports" 字段,还可以定义内部包导入映射,这些映射仅适用于包本身内部的导入说明符。

导入字段中的条目必须始终以 # 开头,以确保它们与包说明符没有歧义。

例如,可以使用导入字段来获得内部模块条件导出的好处:

// package.json
{
  "imports": {
    "#dep": {
      "node": "dep-node-native",
      "default": "./dep-polyfill.js"
    }
  },
  "dependencies": {
    "dep-node-native": "^1.0.0"
  }
}

其中 import '#dep' 没有得到外部包 dep-node-native 的解析(依次包括其导出),而是获取了相对于其他环境中的包的本地文件 ./dep-polyfill.js

"exports" 字段不同,"imports" 字段允许映射到外部包。

导入字段的解析规则与导出字段类似。

Stability: 1 - Experimental

In addition to the "exports" field, it is possible to define internal package import maps that only apply to import specifiers from within the package itself.

Entries in the imports field must always start with # to ensure they are disambiguated from package specifiers.

For example, the imports field can be used to gain the benefits of conditional exports for internal modules:

// package.json
{
  "imports": {
    "#dep": {
      "node": "dep-node-native",
      "default": "./dep-polyfill.js"
    }
  },
  "dependencies": {
    "dep-node-native": "^1.0.0"
  }
}

where import '#dep' does not get the resolution of the external package dep-node-native (including its exports in turn), and instead gets the local file ./dep-polyfill.js relative to the package in other environments.

Unlike the "exports" field, the "imports" field permits mapping to external packages.

The resolution rules for the imports field are otherwise analogous to the exports field.