解析器算法
🌐 Resolver algorithm
加载 ES 模块说明符的算法如下所示,通过下面的 ESM_RESOLVE 方法实现。它返回相对于 parentURL 的模块说明符的解析 URL。
🌐 The algorithm to load an ES module specifier is given through the ESM_RESOLVE method below. It returns the resolved URL for a module specifier relative to a parentURL.
用于确定已解析 URL 的模块格式的算法由 ESM_FORMAT 提供,它会返回任何文件的唯一模块格式。对于 ECMAScript 模块,返回的是 "module" 格式,而 "commonjs" 格式则用于表示通过传统 CommonJS 加载器加载。未来更新中还可以扩展其他格式,例如 "addon"。
🌐 The algorithm to determine the module format of a resolved URL is provided by ESM_FORMAT, which returns the unique module format for any file. The "module" format is returned for an ECMAScript Module, while the "commonjs" format is used to indicate loading through the legacy CommonJS loader. Additional formats such as "addon" can be extended in future updates.
在以下算法中,除非另有说明,所有子程序错误都将被作为这些顶层例程的错误传播。
🌐 In the following algorithms, all subroutine errors are propagated as errors of these top-level routines unless stated otherwise.
defaultConditions 是条件环境名称数组,["node", "import"]。
🌐 defaultConditions is the conditional environment name array,
["node", "import"].
解析器可能会抛出以下错误:
🌐 The resolver can throw the following errors:
- 无效的模块说明符:模块说明符是无效的 URL、包名或包子路径说明符。
- 无效的包配置:package.json 配置无效或包含无效的配置。
- 无效的包目标: 包的导出或导入定义了一个无效类型或字符串目标的包模块。
- 包路径未导出: 包导出未定义或不允许在给定模块的包中使用目标子路径。
- 未定义的包导入: 包导入未定义说明符。
- 模块未找到:请求的包或模块不存在。
- 不支持的目录导入:解析的路径对应于一个目录,该目录不是模块导入的支持目标。