resolve(specifier, context, nextResolve)


  • specifier <string>

  • context <Object>

    • conditions <string[]> 相关 package.json 的导出条件

      ¥conditions <string[]> Export conditions of the relevant package.json

    • importAttributes <Object> 一个对象,其键值对表示要导入的模块的属性

      ¥importAttributes <Object> An object whose key-value pairs represent the attributes for the module to import

    • parentURL <string> | <undefined> 导入此模块的模块,如果这是 Node.js 入口点,则为未定义

      ¥parentURL <string> | <undefined> The module importing this one, or undefined if this is the Node.js entry point

  • nextResolve <Function> 链中后续的 resolve 钩子,或者用户提供的最后一个 resolve 钩子之后的 Node.js 默认 resolve 钩子

    ¥nextResolve <Function> The subsequent resolve hook in the chain, or the Node.js default resolve hook after the last user-supplied resolve hook

    • specifier <string>

    • context <Object> | <undefined> 省略时,将提供默认值。提供时,将优先合并默认值,优先于提供的属性。

      ¥context <Object> | <undefined> When omitted, the defaults are provided. When provided, defaults are merged in with preference to the provided properties.

  • 返回:<Object> | <Promise> 异步版本要么采用包含以下属性的对象,要么采用将解析为此类对象的 Promise。同步版本仅接受同步返回的对象。

    ¥Returns: <Object> | <Promise> The asynchronous version takes either an object containing the following properties, or a Promise that will resolve to such an object. The synchronous version only accepts an object returned synchronously.

    • format <string> | <null> | <undefined>load 钩子的提示(可能会被忽略)。它可以是模块格式(例如 'commonjs''module')或任意值,如 'css''yaml'

      ¥format <string> | <null> | <undefined> A hint to the load hook (it might be ignored). It can be a module format (such as 'commonjs' or 'module') or an arbitrary value like 'css' or 'yaml'.

    • importAttributes <Object> | <undefined> 缓存模块时要使用的导入属性(可选;如果排除,将使用输入)

      ¥importAttributes <Object> | <undefined> The import attributes to use when caching the module (optional; if excluded the input will be used)

    • shortCircuit <undefined> | <boolean> 此钩子打算终止 resolve 钩子链的信号。默认值:false

      ¥shortCircuit <undefined> | <boolean> A signal that this hook intends to terminate the chain of resolve hooks. Default: false

    • url <string> 此输入解析到的绝对 URL

      ¥url <string> The absolute URL to which this input resolves

警告 在异步版本的情况下,尽管支持返回 promise 和异步函数,但对 resolve 的调用仍可能会阻塞主线程,从而影响性能。

¥In the case of the asynchronous version, despite support for returning promises and async functions, calls to resolve may still block the main thread which can impact performance.

resolve 钩子链负责告诉 Node.js 在哪里查找以及如何缓存给定的 import 语句或表达式,或 require 调用。它可以选择返回一种格式(例如 'module')作为 load 钩子的提示。如果指定了格式,load 钩子最终负责提供最终的 format 值(并且可以随意忽略 resolve 提供的提示);如果 resolve 提供 format,则需要自定义 load 钩子,即使只是将值传递给 Node.js 默认的 load 钩子。

¥The resolve hook chain is responsible for telling Node.js where to find and how to cache a given import statement or expression, or require call. It can optionally return a 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.

导入类型属性是缓存键的一部分,用于将加载的模块保存到内部模块缓存中。如果模块应使用与源代码中存在的属性不同的属性进行缓存,则 resolve 钩子负责返回 importAttributes 对象。

¥Import type attributes are part of the cache key for saving loaded modules into the internal module cache. The resolve hook is responsible for returning an importAttributes object if the module should be cached with different attributes than were present in the source code.

context 中的 conditions 属性是一组条件,将用于匹配此解析请求的 包导出条件。它们可用于在别处查找条件映射或在调用默认解析逻辑时修改列表。

¥The conditions property in context is an array of conditions that will be used to match package exports conditions for this resolution request. They can be used for looking up conditional mappings elsewhere or to modify the list when calling the default resolution logic.

当前的 包导出条件 总是在传入钩子的 context.conditions 数组中。为了保证在调用 defaultResolve 时默认的 Node.js 模块说明符解析行为,传递给它的 context.conditions 数组必须包括最初传递到 resolve 钩子的 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.

// Asynchronous version accepted by module.register().
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);
} 
// Synchronous version accepted by module.registerHooks().
function resolve(specifier, context, nextResolve) {
  // Similar to the asynchronous resolve() above, since that one does not have
  // any asynchronous logic.
}