子路径导入


【Subpath imports】

除了"exports"字段外,还有一个包“导入”字段 创建仅适用于从 封装本身。

【In addition to the "exports" field, there is a package "imports" field to create private mappings that only apply to import specifiers from within the package itself.】

"imports" 字段中的条目必须始终以 # 开头,以确保它们与外部包说明符区分开来。

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

例如,imports 字段可以用于为内部模块获得条件导出的好处:

【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"
  }
} 

import '#dep' 无法解析外部包 dep-node-native(包括其导出内容)时,它会在其他环境中获取相对于该包的本地文件 ./dep-polyfill.js

【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.】

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

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

imports 字段的解析规则在其他方面与 exports 字段类似。

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