Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created June 29, 2012 20:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrburke/3020525 to your computer and use it in GitHub Desktop.
Save jrburke/3020525 to your computer and use it in GitHub Desktop.
Module concatenation format

Module concatenation format:

JS module systems, whether commonjs/node, AMD or es harmony modules all allow specifying dependencies inline. Example from node:

var a = require('a'),
    b = require('b');

If this module is combined with other modules into a built file for optimization, how should it be done? It needs some sort of wrapper so that other modules that have a var a in them do not conflict.

AMD is a format that allows that. The r.js optimizer has a flag that allows that to work with plain CommonJS modules so if the developer just wants to always do builds, for any dev change and just use CommonJS modules, that is possible.

So, AMD can be thought of the baseline module interchange format, the desugared form of CommonJS/node modules, that work when concatenated, and from cross domain, where XHR+transform+eval are not possible.

If you are just using browser globals though, then you do not need a concatenation format, but then you also have to work out the dependency tree yourself, and for larger projects that is not enjoyable.

It is also not the future. Any JS module format is going to have these local references to dependencies, and will need some wrapping when doing a build. ES harmony modules currently use module Foo { /* code in here */ }, which is analogous to define('Foo', function(require) { }) in AMD.

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