Harmony modules feedback

James Burke jrburke at gmail.com
Tue Jan 17 09:20:51 PST 2012


On Tue, Jan 17, 2012 at 3:34 AM, Mariusz Nowak
<medikoo+mozilla.org at medikoo.com> wrote:
> James Burke-5 wrote:
>> This is provably false. You normally do not need hundreds of modules
>> to build a site.
>>
>
> I wasn't theorizing, I was talking about real applications that are already
> produced.

What I was objecting to is the characterization that an app that loads
hundreds of module would not work and therefore builds are always
needed for all-sized apps. Finding one or two pathological cases does
not prove the need for builds for all people, and I have experience
that points otherwise, even for larger apps. Maybe you did not mean to
take the connection that far, but that is how I read it.

> I think Harmony modules have more in common with CommonJS than with AMD, and
> transition from CommonJS will be easier than from AMD. See slides 86-89 from
> http://www.slideshare.net/medikoo/javascript-modules-done-right (it's
> presentation I've done once on local Warsaw meetup)

CommonJS modules have an imperative require, that is not something
that will work in Harmony modules. AMD does not, which matches the
current harmony proposal more closely. In other words, you can do this
today in CommonJS, but this is not translatable directly to harmony
modules:

var a = require(someCondition ? 'a' : 'a1');

In the current harmony proposals, you would need to use the
callback-style of the module_loaders API to load the module. This
implies an async resolution of the dependency, which likely changes
the above module's API to the outside world.

I agree that the surface syntax of CommonJS looks more like Harmony
modules, but the transform of AMD to harmony modules is really not
that much harder, and translating from vanilla AMD modules (ones that
do not use loader plugins) does not have the kinds of conversion where
the module needs to be manually re-architected, as mentioned above.

> Of course Harmony will allow you to load each module separately, but with
> default syntax I understand it will work synchronously, I'm not sure we will
> do that for large number of modules. For asynchronous loading you may use
> dynamic loader which I think would be great to load bigger module
> dependencies.

Harmony modules will parse the module code, pull out the module
dependencies, load them, do some compiling/linking, then execute the
code. This is similar to what AMD does with this form:

define(function(require) {
    var a = require('a');
});

AMD loaders use Function.prototype.toString() and then parse that
function body to find the 'a' dependency, fetches and executes 'a',
then execute this function. Of course this is a bit "low-tech" and
having an JS engine get a real parse tree before code execution is
better. But the end behavior, as far as network traffic, is the same.

James


More information about the es-discuss mailing list