Skip to content

Asynchronous module loading #30

Description

@divmain

Add feature similar to require.ensure, but call it require.async.

  • These function calls should be detected in much the same way as require(...) calls.
  • Modules should still be recursively discovered and included in build.
  • Build-time behavior should be identical - require.async has no effect on splitting. That is still managed by the build config itself.
  • Run-time behavior is different - module is not included in dependencies hash, and is not guaranteed to have been invoked prior to containing module.
  • When require.async (or compiled equivalent) is invoked at run-time, 1) modules are resolved and invoked, 2) callback is invoked with params matching the required modules.

Example input

const libA = require("./lib-a");
require.async(["./lib-b", "./lib-c"], function (libB, libC) {
  // ...
});

Example output

{
  'aaaaaa': {
    deps: [],
    fn: function (require, module, exports) {
      var libA = require("bbbbbb")
      require.async("cccccc"/*["dddddd", "eeeeee"]*/, function (libB, libC) {
        // ...
      });
    }
  },
  'bbbbbb': {
    deps: [],
    fn: function (require, module, exports) {
      module.exports = "libA";
    }
  },
  'cccccc': {
    deps: ["dddddd", "eeeeee"],
    fn: function (require, module, exports) {
      module.exports = function (cb) {
        cb(require("dddddd"), require("eeeeee"));
      };
    }
  },
  'dddddd': {
    deps: [],
    fn: function (require, module, exports) {
      module.exports = "libB";
    }
  },
  'eeeeee': {
    deps: [],
    fn: function (require, module, exports) {
      module.exports = "libC";
    }
  }
}

When the require.async is invoked within the example module, the Interlock will call the cccccc module as if it were an entry point - first ensuring that all dependencies are installed and invoked, resolving/installing/invoking any missing dependencies, and finally calling the cccccc module itself. This module's exports will be a function that invokes a callback, requiring and passing constituent dependencies as parameters.

The require.async implementation will have recorded the callback passed to it, and pass that to cccccc's returned function when available.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions