Static Module Resolution

Brendan Eich brendan at mozilla.org
Mon Jul 2 11:09:26 PDT 2012


Kevin Smith wrote:
> This solution is less ideal than being able to use import for legacy 
> modules, but gets mad points for simplicity.
>
> The next question is this:  what will happen if "require" is used to 
> load an ES6 module (specifically, a module that contains import 
> statements)?
>
> Dave, is it possible to create an effectively synchronous loader using 
> the Loader API?
>
>     let syncLoader = new Loader(parent, {
>
>       fetch: function(relURL, baseURL, request, resolved) {
>         let src = fetchSourceFromDisk(resolved);
>         request.fulfill(src); // Fulfill in same turn of the event loop
>       }
>
>     }
>
>     syncLoader.load("abc");
>
>     // Is ABC ready at this point?
>     let ABC = syncLoader.get("abc");

This came up at the March TC39 meeting. Oliver Hunt of Apple pointed out 
that we do not want a sync (as in block rendering) footgun from <script> 
tags, a la sync XHR. See

https://mail.mozilla.org/pipermail/es-discuss/2012-March/021872.html

Quoting

DaveH proposal: Bifurcate grammar so that scripts (whether in<script>
tags or in files included from those) cannot contain static module
load statements, not even in syntactically nested modules.  Modules
can include such statements but can only be parsed via dynamic loads.
<script>
System.load("interp.js", function(m) {...});
</script>

interp.js contains:
module stack = "stack.js";
export function evaluate(...) {...}

stack.js contains:
export class Stack {...}

The body of an eval would be treated like a script body, not a module
body.  This avoids the tarpit of dealing with synchronous i/o in eval.


--- end quote ---

Again, sync loading is anathema in the browser. Any require usage must 
be (1) callback-based, or else (2) based on a preprocessor (if not full 
CPS-transforming compiler).

In case (1), let's say the underlying system (AMD) uses XHR and eval. 
The March meeting's resolution would throw an early error (early in the 
eval's phase structure, later in the script's phase structure) on any 
import syntax in the eval'ed program.

In case (2), the require dependency is loaded before runtime, one way or 
another. If the compiler generates ES6, no problem. If it generates ES5, 
also no problem (ES6 -> ES5 compilation entails compiling import into 
something like what require compiles to).

/be

> - Kevin
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list