Note: The loaders API is being redesigned. This hook may disappear or its signature may change. Do not rely on the API described below.

    • Returns: {string}

    Sometimes it might be necessary to run some code inside of the same global scope that the application runs in. This hook allows the return of a string that is run as sloppy-mode script on startup.

    Similar to how CommonJS wrappers work, the code runs in an implicit function scope. The only argument is a require-like function that can be used to load builtins like “fs”: getBuiltin(request: string).

    If the code needs more advanced require features, it has to construct its own require using module.createRequire().

    1. /**
    2. * @returns {string} Code to run before application startup
    3. */
    4. export function getGlobalPreloadCode() {
    5. return `\
    6. globalThis.someInjectedProperty = 42;
    7. console.log('I just set some globals!');
    8. const { createRequire } = getBuiltin('module');
    9. const require = createRequire(process.cwd() + '/<preload>');
    10. // [...]
    11. `;
    12. }