resolve(specifier, context, nextResolve)
加载器 API 正在重新设计。此钩子可能会消失或其签名可能会更改。请不要依赖下面描述的 API。
specifier<string>context<Object>conditions<string[]> 相关package.json的导出条件importAssertions<Object>parentURL<string> | <undefined> 导入此模块的模块,如果这是 Node.js 入口点,则为 undefined
nextResolve<Function> 链中后续的resolve钩子,或者在最后一个用户提供的resolve钩子之后的 Node.js 默认resolve钩子- 返回值: <Object>
format<string> | <null> | <undefined> 对 load 钩子的提示(可能会被忽略)'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'shortCircuit<undefined> | <boolean> 一个信号,表示此钩子打算终止resolve钩子链。默认:falseurl<string> 该输入解析到的绝对 URL
resolve 钩子链负责为给定的模块说明符和父 URL 解析文件 URL,并可选择提供其格式(例如 'module')作为 load 钩子的提示。如果指定了格式,load 钩子最终负责提供最终的 format 值(并且可以选择忽略 resolve 提供的提示);如果 resolve 提供了 format,即使只是为了将该值传递给 Node.js 默认的 load 钩子,也需要自定义 load 钩子。
【The resolve hook chain is responsible for resolving file URL for a given
module specifier and parent URL, and optionally its format (such as 'module')
as a hint to the load hook. If a format is specified, the load hook is
ultimately responsible for providing the final format value (and it is free to
ignore the hint provided by resolve); if resolve provides a format, a
custom load hook is required even if only to pass the value to the Node.js
default load hook.】
模块指定符是在 import 语句或 import() 表达式中的字符串。
【The module specifier is the string in an import statement or
import() expression.】
父 URL 是导入此模块的模块的 URL,如果这是应用的主入口点,则为 undefined。
【The parent URL is the URL of the module that imported this one, or undefined
if this is the main entry point for the application.】
context 中的 conditions 属性是一个适用于此解析请求的 包导出条件 条件数组。它们可以用于在其他地方查找条件映射,或者在调用默认解析逻辑时修改列表。
【The conditions property in context is an array of conditions for
package exports conditions that apply to this resolution
request. They can be used for looking up conditional mappings elsewhere or to
modify the list when calling the default resolution logic.】
当前的 包导出条件 总是包含在传入 hook 的 context.conditions 数组中。为了在调用 defaultResolve 时保证 默认的 Node.js 模块指定符解析行为,传递给它的 context.conditions 数组 必须 包含最初传入 resolve hook 的 context.conditions 数组的 所有 元素。
【The current package exports conditions are always in
the context.conditions array passed into the hook. To guarantee default
Node.js module specifier resolution behavior when calling defaultResolve, the
context.conditions array passed to it must include all elements of the
context.conditions array originally passed into the resolve hook.】
export async function resolve(specifier, context, nextResolve) {
const { parentURL = null } = context;
if (Math.random() > 0.5) { // Some condition.
// For some or all specifiers, do some custom logic for resolving.
// Always return an object of the form {url: <string>}.
return {
shortCircuit: true,
url: parentURL ?
new URL(specifier, parentURL).href :
new URL(specifier).href,
};
}
if (Math.random() < 0.5) { // Another condition.
// When calling `defaultResolve`, the arguments can be modified. In this
// case it's adding another value for matching conditional exports.
return nextResolve(specifier, {
...context,
conditions: [...context.conditions, 'another-condition'],
});
}
// Defer to the next hook in the chain, which would be the
// Node.js default resolve if this is the last user-specified loader.
return nextResolve(specifier);
}