Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Last active October 7, 2023 23:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorendorff/fc5bad969137402caa10bb3570b3f202 to your computer and use it in GitHub Desktop.
Save jorendorff/fc5bad969137402caa10bb3570b3f202 to your computer and use it in GitHub Desktop.

This is a sketch of how module loading might work in HTML.

  1. A "load task" is the end-to-end job of loading a module and all its dependencies, linking everything together, and running the code. It's a high-level concept.

    Load tasks come in from <script type=module> elements, plus wherever else (maybe a DOM API). The loader can have multiple load tasks in flight at once.

  2. The loader contains a collection of modules shared by all load tasks. Each module is in one of these states:

    • loading - waiting for the network;
    • waiting for dependencies - loaded and parsed, not linked;
    • ready to link - loaded and parsed, with all dependencies either ready-to-link or complete; or
    • complete - fully linked, ready to evaluate or already evaluated
  3. Linking happens as eagerly as possible whenever a module arrives off the network. This means that very often we'll be linking one module at a time. In any case, every link set will be a strongly connected component of ready-to-link modules in the dependency graph; and therefore if linking fails, we should discard all the modules that we were trying to link.

  4. When a link error or any other error happens, we immediately discard the failed module and everything that depends on it (potentially many modules in various states, and load tasks).

  5. Module body evaluation can happen either as eagerly as possible (like linking) or only at the end, when a load task has got everything it needs (the original target module becomes "complete"). The latter approach is a bit more deterministic in terms of what order modules evaluate; maybe we care, maybe not.

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