ES5 Module Systems (was: Alternative proposal to privateName.public)

Axel Rauschmayer axel at rauschma.de
Mon Dec 26 14:03:28 PST 2011


> Hi Axel, a nice demonstration of the simplicity of the core of AMD is the simplicity with which it can be implemented using a promise library.

Exactly; nice demo. Doing something comparably asynchronous with Node.js modules (without a compilation step) is much more hacky:
http://www.2ality.com/2011/11/lobrow.html

> 
>     ({ define: typeof define === "function"
>         ? define
>         : function(A,F) { module.exports = F.apply(null, A.map(require)) } }).
>     define([ "./module1", "./module2" ],
>         function (module1, module2) {
>             return ...
>         }
>     );
> 
> [1] http://www.2ality.com/2011/11/module-gap.html
> 
> 
> I like this adapter, and have just changed <http://code.google.com/p/es-lab/source/browse/trunk/src/ses/amdTest3.js> to test that it works with our simple AMD Loader in translation-free SES-on-ES5 (similar to the above, at <http://code.google.com/p/es-lab/source/browse/trunk/src/ses/makeSimpleAMDLoader.js>). You can run this test by visiting <http://es-lab.googlecode.com/svn/trunk/src/ses/explicit.html> in a modern browser. If you see the line "AMD loader test...succeeded", then it did.
> 
> However, I am confused by the "module.exports = ..." part of your boilerplate. The main CommonJS wiki seems down at the moment, but looking at <http://wiki.commonjs.org.mirrors.page.ca/articles/m/o/d/Modules.html> on the mirror site, I could not find any support for this idiom. The closest I could find was <http://wiki.commonjs.org.mirrors.page.ca/articles/m/o/d/Modules_SetExports_9215.html>, which suggests it should read "module.setExports(...);" instead. Where does "module.exports = ..." come from?


It’s necessary for Node.js.

Node.js:

    var module1 = require("./module1");
    module1.foo();
    var module2 = require("./module2");
    export.bar = function() {
        module2.baz();
    }

AMD:

    define([ "./module1", "./module2" ],
        function (module1, module2) {
            module1.foo();
            return { // export
                bar: function () {
                    module2.baz();
                }
            };
        }
    );

Hence, unless you add to `exports`, Node.js won’t see your module’s contributions.

IIRC, Node.js deviates from CJS in that module.exports is an alias for exports. Without that alias, the boilerplate would be more complicated (you’d have to copy the properties of the object produced by F() over to exports). But I am focusing on Node.js, at the moment, so I can afford to rely on this functionality.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111226/945faf4e/attachment.html>


More information about the es-discuss mailing list