Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Forked from millermedeiros/universal-module.js
Created September 30, 2011 18:02
Show Gist options
  • Select an option

  • Save briancavalier/1254517 to your computer and use it in GitHub Desktop.

Select an option

Save briancavalier/1254517 to your computer and use it in GitHub Desktop.
Universal JavaScript Module, supports AMD (RequireJS), Node.js, and the browser.
// Define a global define() that works in the current environment
(function(global) {
var define;
define = global.define;
// If AMD, just use existing global.define
if(!(typeof define === 'function' && define.amd)) {
if(typeof require === 'function' && typeof module !== 'undefined' && module.exports) {
//CommonJS
define = function(deps, factory) {
module.exports = factory.apply(this, deps.map(require));
};
} else {
//Browser (regular script tag)
define = function(name, deps, factory){
var d, i = 0, old = global[name], mod;
while(d = deps[i]){
deps[i++] = this[d];
}
global[name] = mod = factory.apply(global, deps);
mod.noConflict = function(){
global[name] = old;
return mod;
};
};
}
}
global.define = define;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment