supporting both CommonJS and AMD

slobo picture slobo · Dec 2, 2012 · Viewed 10.1k times · Source

Is there a way to create a javascript micro-library (a library that has no dependencies), that support all of the following module formats:

  • Asynchronous Module Definition
  • CommonJS
  • exposing the library's exports as a global namespace object (no loader)

Answer

Marc picture Marc · Dec 25, 2012

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 module.exports
    return {
        sayHi: function(name) {
            console.log('Hi ' + name + '!');
        }
    };
}));

This can then be used:

  1. in AMD (e.g. with requireJS):

    requirejs(['mod'], function(mod) {
        mod.sayHi('Marc');
    });
    
  2. in commonJS (e.g. nodeJS):

    var mod = require('./mod');
    mod.sayHi('Marc');
    
  3. globally (e.g. in HTML):

    <script src="mod.js"></script>
    <script>mod.sayHi('Marc');</script>
    

This method needs to get more publicity - if jQuery and co. started using it life would be much easier!