Last active
August 29, 2015 13:56
-
-
Save collingo/9272456 to your computer and use it in GitHub Desktop.
Simple UMD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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