解析器算法


加载 ES 模块说明符的算法通过下面的 ESM_RESOLVE 方法给出。 它返回相对于 parentURL 的模块说明符的解析 URL。

确定解析 URL 的模块格式的算法由 ESM_FORMAT 提供,它返回任何文件的唯一模块格式。 "module" 格式为 ECMAScript 模块返回,而 "commonjs" 格式用于指示通过旧版 CommonJS 加载器加载。 其他格式,如 "addon" 可以在未来的更新中扩展。

在以下算法中,除非另有说明,否则所有子程序错误都将作为这些顶层程序的错误传播。

defaultConditions 是条件环境名称数组,["node", "import"]

解析器可能会抛出以下错误:

  • 无效的模块说明符:模块说明符是无效的 URL、包名称、或包子路径说明符。
  • 无效的包配置:package.json 配置无效或包含无效配置。
  • 无效的包目标:包导出或导入为无效类型或字符串目标的包定义了目标模块。
  • 未导出包路径:包导出未定义或允许给定模块的包中的目标子路径。
  • 未定义包导入:包导入未定义说明符。
  • 未找到模块:请求的包或模块不存在。

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.

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 is the conditional environment name array, ["node", "import"].

The resolver can throw the following errors:

  • Invalid Module Specifier: Module specifier is an invalid URL, package name or package subpath specifier.
  • Invalid Package Configuration: package.json configuration is invalid or contains an invalid configuration.
  • Invalid Package Target: Package exports or imports define a target module for the package that is an invalid type or string target.
  • Package Path Not Exported: Package exports do not define or permit a target subpath in the package for the given module.
  • Package Import Not Defined: Package imports do not define the specifier.
  • Module Not Found: The package or module requested does not exist.