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}
      • format {string}
    • defaultGetSource {Function}
    • Returns: {Object}
      • source {string|SharedArrayBuffer|Uint8Array}

    The getSource hook provides a way to define a custom method for retrieving the source code of an ES module specifier. This would allow a loader to potentially avoid reading files from disk.

    1. /**
    2. * @param {string} url
    3. * @param {{ format: string }} context
    4. * @param {Function} defaultGetSource
    5. * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>}
    6. */
    7. export async function getSource(url, context, defaultGetSource) {
    8. const { format } = context;
    9. if (Math.random() > 0.5) { // Some condition.
    10. // For some or all URLs, do some custom logic for retrieving the source.
    11. // Always return an object of the form {source: <string|buffer>}.
    12. return {
    13. source: '...',
    14. };
    15. }
    16. // Defer to Node.js for all other URLs.
    17. return defaultGetSource(url, context, defaultGetSource);
    18. }