Skip to content

Instantly share code, notes, and snippets.

@collingo
Last active August 29, 2015 13:56
Show Gist options
  • Save collingo/9272456 to your computer and use it in GitHub Desktop.
Save collingo/9272456 to your computer and use it in GitHub Desktop.
Simple UMD
// resuable shortcut for module definition that works in Node (cjs) and the browser (Requirejs)
// (to save boilerplate may consider making umd function a global var - I know, I know!)
var umd = function (root, dependencies, factory) {
var requiredDependencies = [];
if (typeof define === 'function' && define.amd) {
define(dependencies, factory);
} else if (typeof exports === 'object') {
for (var i = dependencies.length - 1; i >= 0; i--) {
dependencies[i] = require(dependencies[i]);
}
module.exports = factory.apply(this, dependencies);
}
}.bind(this, this);
// if umd is global then you only require this for module definition in node/browser
umd(['a', 'b'], function (a, b) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment