Mostly about Javascript, Ruby on Rails and other web stuff

AMD, Is Not One or the Other

Dec 5th, 2012

I am starting to see a pattern in many JS libraries where they are trying to detect the presence of AMD an act accordingly. I think it is great that AMD is getting so much attention but they usual way this is done has a big issue: It shouldn’t be one or the other.

Problem with this is that if the script is loaded using a common script tag and a AMD loaded (e.g. require) is also present, then it won’t be possible to use the library in the normal way (using the global variable).

You might think that if require is there we should load everything using it, but we have good reasons not to do this. We concatentate and minifiy the most important parts of our JS code, the rest is loaded using require on demand.

So the better way to do this is by attaching the library in many ways as possible:

12345678910111213141516171819

// CommonJS module is definedif(typeofexports!=='undefined'){if(typeofmodule!=='undefined'&&module.exports){module.exports=foo;}exports.foo=foo;}// Register as a named module with AMD.if(typeofdefine==='function'&&define.amd){define(‘foo’,[],function(){returnfoo;});}// Integrate with $ if defined// or create our own underscore object.if(jQuery){jQuery.fn.foo=foo}