supporting both CommonJS and AMD

Yes, and I owe this answer to ded and his awesome modules: (function(name, definition) { if (typeof module != ‘undefined’) module.exports = definition(); else if (typeof define == ‘function’ && typeof define.amd == ‘object’) define(definition); else this[name] = definition(); }(‘mod’, function() { //This is the code you would normally have inside define() or add to … Read more

Requirejs why and when to use shim config

A primary use of shim is with libraries that don’t support AMD, but you need to manage their dependencies. For example, in the Backbone and Underscore example above: you know that Backbone requires Underscore, so suppose you wrote your code like this: require([‘underscore’, ‘backbone’] , function( Underscore, Backbone ) { // do something with Backbone … Read more

Relation between CommonJS, AMD and RequireJS?

RequireJS implements the AMD API (source). CommonJS is a way of defining modules with the help of an exports object, that defines the module contents. Simply put, a CommonJS implementation might work like this: // someModule.js exports.doSomething = function() { return “foo”; }; //otherModule.js var someModule = require(‘someModule’); // in the vein of node exports.doSomethingElse … Read more