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 [ArrayBuffer][] object is a [SharedArrayBuffer][].
    • The specific [TypedArray][] object is a [Uint8Array][].

    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][].

    1. /**
    2. * @param {string} url
    3. * @param {Object} context (currently empty)
    4. * @param {Function} defaultGetFormat
    5. * @returns {Promise<{ format: string }>}
    6. */
    7. export async function getFormat(url, context, defaultGetFormat) {
    8. if (Math.random() > 0.5) { // Some condition.
    9. // For some or all URLs, do some custom logic for determining format.
    10. // Always return an object of the form {format: <string>}, where the
    11. // format is one of the strings in the preceding table.
    12. return {
    13. format: 'module',
    14. };
    15. }
    16. // Defer to Node.js for all other URLs.
    17. return defaultGetFormat(url, context, defaultGetFormat);
    18. }