load(url, context, defaultLoad)
加載程序 API 正在重新設計。這個鉤子可能會消失,或者它的簽名可能會改變。不要依賴下麵說明的 API。
在此 API 的先前版本中,它被拆分為 3 個單獨的、現已棄用的鉤子(getFormat
、getSource
和 transformSource
)。
url
<string>context
<Object>format
<string> | <null> | <undefined>resolve
鉤子可選提供的格式。importAssertions
<Object>
defaultLoad
<Function>- 返回:<Object>
format
<string>source
<string> | <ArrayBuffer> | <TypedArray>
load
鉤子提供了一種方法來定義自定義方法來確定應如何解釋、檢索和解析 URL。它還負責驗證導入斷言。
format
的最終值必須是以下之一:
format | 說明 | load 返回的 source 的可接受類型 |
---|---|---|
'builtin' | 加載 Node.js 內置模塊 | 不適用 |
'commonjs' | 加載一個 Node.js CommonJS 模塊 | 不適用 |
'json' | 加載 JSON 文件 | { , , } |
'module' | 加載一個 ES 模塊 | { , , } |
'wasm' | 加載 WebAssembly 模塊 | { , } |
source
的值對於類型 'builtin'
被忽略,因為目前無法替換 Node.js 內置(核心)模塊的值。 source
的值對於類型 'commonjs'
被忽略,因為 CommonJS 模塊加載器沒有為 ES 模塊加載器提供覆蓋 CommonJS module return value 的機製。未來可能會克服這一限製。
警告:ESM load
鉤子和來自 CommonJS 模塊的命名空間導出不兼容。嘗試將它們一起使用將導致導入中的空對象。這可能會在未來得到解決。
這些類型都對應於 ECMAScript 中定義的類。
- 特定的
ArrayBuffer
SharedArrayBuffer
- 特定的
TypedArray
Uint8Array
如果基於文本的格式(即 'json'
、 'module'
)的源值不是字符串,則使用
將其轉換為字符串。util.TextDecoder
load
鉤子提供了一種方法來定義自定義方法來檢索 ES 模塊說明符的源代碼。這將允許加載程序潛在地避免從磁盤讀取文件。它還可用於將無法識別的格式映射到受支持的格式,例如 yaml
到 module
。
/**
* @param {string} url
* @param {{
format: string,
}} context If resolve settled with a `format`, that value is included here.
* @param {Function} defaultLoad
* @returns {Promise<{
format: string,
source: string | ArrayBuffer | SharedArrayBuffer | Uint8Array,
}>}
*/
export async function load(url, context, defaultLoad) {
const { format } = context;
if (Math.random() > 0.5) { // Some condition.
/*
For some or all URLs, do some custom logic for retrieving the source.
Always return an object of the form {
format: <string>,
source: <string|buffer>,
}.
*/
return {
format,
source: '...',
};
}
// Defer to Node.js for all other URLs.
return defaultLoad(url, context, defaultLoad);
}
在更高級的場景中,這也可用於將不受支持的源轉換為受支持的源(參見下麵的Examples)。
相關用法
- Node.js lodash.sortBy()用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
- Node.js http2.Http2ServerRequest request.url用法及代碼示例
- Node.js request.socket用法及代碼示例
- Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
- Node.js tlsSocket.authorized用法及代碼示例
- Node.js zlib.deflateRaw()用法及代碼示例
- Node.js http.IncomingMessage message.rawHeaders用法及代碼示例
- Node.js Console用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js URL.protocol用法及代碼示例
- Node.js http.Agent.reuseSocket(socket, request)用法及代碼示例
- Node.js fs.filehandle.datasync()用法及代碼示例
- Node.js socket.bind()用法及代碼示例
- Node.js v8.getHeapSpaceStatistics()用法及代碼示例
- Node.js http2session.destroyed用法及代碼示例
- Node.js http.ServerResponse response.statusCode用法及代碼示例
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
- Node.js Http2ServerResponse.finished用法及代碼示例
- Node.js Http2Stream close用法及代碼示例
- Node.js readStream.isRaw用法及代碼示例
- Node.js diffieHellman.getGenerator()用法及代碼示例
- Node.js util.types.isInt16Array(value)用法及代碼示例
- Node.js certificate.verifySpkac(spkac[, encoding])用法及代碼示例
- Node.js util.types.isNativeError(value)用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 load(url, context, defaultLoad)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。