Fork me on GitHub

UMD template

'UMD' template is based on a familiar standardized UMD template with optional global exports inspired by returnExportsGlobal.js.

With a source module like this (coffeescript)

_ = require 'lodash'
myModule = {...}
module.exports = myModule

It generates a converted UMD module like this :

    // Generated by uRequire v0.4.0beta2
      var __isAMD = (typeof define === 'function' && define.amd),
        __isNode = (typeof exports === 'object'),
        __isWeb = !__isNode;
      (function(root, factory){
        if (typeof exports === 'object') {
          var nr = new (require('urequire').NodeRequirer)('myModule', module, __dirname, '.');
          module.exports = factory(nr.require, exports, module, nr.require('lodash'));
        } else if (typeof define === 'function' && define.amd) {
          define(['require', 'exports', 'module', 'lodash'], factory);
      })(this, function(require, exports, module, _){

        // uRequire: start body of original nodejs module
        var myModule, _;
        _ = require("lodash");
        myModule = {...};
        module.exports = myModule;
        // uRequire: end body of original nodejs module

        return module.exports;

You shouldn't be intimidated by the sheer size increase; It's not such a great % of increase for real life larger modules. Also you usually build to individual UMD files only for development and then you optimize or combine them for production.

Note: 'UMD' template needs 'urequire' dependency to run on node as it uses new require('urequire').NodeRequirer to actually require modules. If you want a UMD template without this dependency use 'UMDplain' instead.