getFormat(url, context, defaultGetFormat)
注意:加载器 API 正在重新设计。 这个钩子可能会消失,或者它的签名可能会改变。 不要依赖下面描述的 API。
url<string>context<Object>defaultGetFormat<Function>- 返回: <Object>
format<string>
 
getFormat 钩子提供了一种方式来定义确定网址应如何解释的自定义方法。
返回的 format 也会影响解析时模块的可接受的源值形式。
这可以是以下之一:
format | 描述 | getSource 或 transformSource 返回的 source 的可接受类型 | 
|---|---|---|
'builtin' | 加载 Node.js 内置模块 | 不适用 | 
'commonjs' | 加载 Node.js CommonJS 模块 | 不适用 | 
'json' | 加载 JSON 文件 | { string, ArrayBuffer, TypedArray } | 
'module' | 加载 ES 模块 | { string, ArrayBuffer, TypedArray } | 
'wasm' | 加载 WebAssembly 模块 | { ArrayBuffer, TypedArray } | 
注意:这些类型都对应于 ECMAScript 中定义的类。
- 特定的 
ArrayBuffer对象是SharedArrayBuffer。 - 特定的 
TypedArray对象是Uint8Array。 
注意:如果基于文本的格式(即 'json'、'module')的源值不是字符串,则使用 util.TextDecoder 将其转换为字符串。
/**
 * @param {string} url
 * @param {Object} context (currently empty)
 * @param {Function} defaultGetFormat
 * @returns {Promise<{ format: string }>}
 */
export async function getFormat(url, context, defaultGetFormat) {
  if (Math.random() > 0.5) { // 一些条件。
    // 对于部分或所有 URL,执行一些自定义逻辑来确定格式。
    // 始终返回 {format: <string>} 形式的对象,
    // 其中格式是上表中的字符串之一。
    return {
      format: 'module',
    };
  }
  // 所有其他 URL 都遵循 Node.js。
  return defaultGetFormat(url, context, defaultGetFormat);
}Note: The loaders API is being redesigned. This hook may disappear or its signature may change. Do not rely on the API described below.
url<string>context<Object>defaultGetFormat<Function>- Returns: <Object>
format<string>
 
The getFormat hook provides a way to define a custom method of determining how
a URL should be interpreted. The format returned also affects what the
acceptable forms of source values are for a module when parsing. This can be one
of the following:
format | Description | Acceptable Types For source Returned by getSource or transformSource | 
|---|---|---|
'builtin' | Load a Node.js builtin module | Not applicable | 
'commonjs' | Load a Node.js CommonJS module | Not applicable | 
'json' | Load a JSON file | { string, ArrayBuffer, TypedArray } | 
'module' | Load an ES module | { string, ArrayBuffer, TypedArray } | 
'wasm' | Load a WebAssembly module | { ArrayBuffer, TypedArray } | 
Note: These types all correspond to classes defined in ECMAScript.
- The specific 
ArrayBufferobject is aSharedArrayBuffer. - The specific 
TypedArrayobject is aUint8Array. 
Note: If the source value of a text-based format (i.e., 'json', 'module') is
not a string, it is converted to a string using util.TextDecoder.
/**
 * @param {string} url
 * @param {Object} context (currently empty)
 * @param {Function} defaultGetFormat
 * @returns {Promise<{ format: string }>}
 */
export async function getFormat(url, context, defaultGetFormat) {
  if (Math.random() > 0.5) { // Some condition.
    // For some or all URLs, do some custom logic for determining format.
    // Always return an object of the form {format: <string>}, where the
    // format is one of the strings in the preceding table.
    return {
      format: 'module',
    };
  }
  // Defer to Node.js for all other URLs.
  return defaultGetFormat(url, context, defaultGetFormat);
}