jorendorff / js-loaders

Pseudoimplementation of the proposed ES6 module loaders.
54 stars 7 forks source link

Bundling #90

Closed guybedford closed 10 years ago

guybedford commented 10 years ago

@unscriptable raised some points about bundling before. I want to continue this discussion here.

Bundling can be done with Loader.prototype.define providing a sequence of source strings:

  System.define('another-module', "import 'jquery'; export var p");
  System.define('module-name', "export ...");

As soon as such a bundle executes, it will route any imports to its own definitions. This is entirely compatible with circular references.

The alternative to this is to use HTML imports with something like:

  <module name="module-name">
    import 'q';
    export ...
  </module>
  <module name="another-module">
    export var p;
  </module>

The two methods are mostly identical though, just that the syntax is different.

We may have code somewhere that does System.import('module-name)`, and we want to either ensure that load itself triggers loading the bundle, OR that we have definitely loaded the bundle before that load runs.

Dropping a script tag in the head isn't enough because this will block the page, making the production experience worse.

We can actually use paths (I think this is what @unscriptable was referring to, unless I am mistaken) to direct to the right bundle:

  System.paths['module-name'] = 'some/bundle';
  System.paths['another-module'] = 'some/bundle;

This results in the locate hook loading the bundle, and when executed the bundle will define the things it needs.

The issue though is that the define call won't pick up the existing load record for the module and move it along, but rather throw an error because the module is already loading.

An adjustment to the algorithm could potentially take this into account and pick up the load record where it was in define, or return fulfilled if the module was already there. Throwing errors in define will otherwise happen way too easily for common bundling needs.

I'd be very interested to hear what the thoughts are on this currently, and how differently we're thinking about this situation.