resolve(specifier, context, defaultResolve)
注意:加载器 API 正在重新设计。 这个钩子可能会消失,或者它的签名可能会改变。 不要依赖下面描述的 API。
specifier
<string>context
<Object>conditions
<string[]>parentURL
<string>
defaultResolve
<Function>- 返回: <Object>
url
<string>
resolve
钩子返回给定模块说明符和父 URL 的解析文件 URL。
模块说明符是 import
语句或 import()
表达式中的字符串,父 URL 是导入此模块的 URL,如果这是应用程序的主要入口点,则为 undefined
context
上的 conditions
属性是适用于此解析请求的条件导出的条件数组。
它们可用于在别处查找条件映射或在调用默认解析逻辑时修改列表。
当前的包导出条件始终在传入钩子的 context.conditions
数组中。
为了在调用 defaultResolve
时保证默认的 Node.js 模块说明符解析行为,传给它的 context.conditions
数组必须包含最初传到 resolve
钩子的 context.conditions
数组的所有元素。
/**
* @param {string} specifier
* @param {{
* conditions: !Array<string>,
* parentURL: !(string | undefined),
* }} context
* @param {Function} defaultResolve
* @returns {Promise<{ url: string }>}
*/
export async function resolve(specifier, context, defaultResolve) {
const { parentURL = null } = context;
if (Math.random() > 0.5) { // 一些条件。
// 对于部分或全部说明符,做一些自定义逻辑来解决。
// 总是返回 {url: <string>} 形式的对象。
return {
url: parentURL ?
new URL(specifier, parentURL).href :
new URL(specifier).href,
};
}
if (Math.random() < 0.5) { // 另一个条件。
// 当调用 `defaultResolve` 时,可以修改参数。
// 在这种情况下,它为匹配条件导出添加了另一个值。
return defaultResolve(specifier, {
...context,
conditions: [...context.conditions, 'another-condition'],
});
}
// 对于所有其他说明符,请遵循 Node.js。
return defaultResolve(specifier, context, defaultResolve);
}
Note: The loaders API is being redesigned. This hook may disappear or its signature may change. Do not rely on the API described below.
specifier
<string>context
<Object>conditions
<string[]>parentURL
<string>
defaultResolve
<Function>- Returns: <Object>
url
<string>
The resolve
hook returns the resolved file URL for a given module specifier
and parent URL. The module specifier is the string in an import
statement or
import()
expression, and 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.
The conditions
property on the context
is an array of conditions for
Conditional exports 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.
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.
/**
* @param {string} specifier
* @param {{
* conditions: !Array<string>,
* parentURL: !(string | undefined),
* }} context
* @param {Function} defaultResolve
* @returns {Promise<{ url: string }>}
*/
export async function resolve(specifier, context, defaultResolve) {
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 {
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 defaultResolve(specifier, {
...context,
conditions: [...context.conditions, 'another-condition'],
});
}
// Defer to Node.js for all other specifiers.
return defaultResolve(specifier, context, defaultResolve);
}