子路径导出
¥Subpath exports
当使用 "exports" 字段时,可以通过将主入口点视为 "." 子路径来定义自定义子路径以及主入口点:
¥When using the "exports" field, custom subpaths can be defined along
with the main entry point by treating the main entry point as the
"." subpath:
{
"exports": {
".": "./index.js",
"./submodule.js": "./src/submodule.js"
}
} 现在消费者只能导入 "exports" 中定义的子路径:
¥Now only the defined subpath in "exports" can be imported by a consumer:
import submodule from 'es-module-package/submodule.js';
// Loads ./node_modules/es-module-package/src/submodule.js 而其他子路径会出错:
¥While other subpaths will error:
import submodule from 'es-module-package/private-module.js';
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED