Created
October 4, 2011 21:26
-
-
Save jrburke/1262861 to your computer and use it in GitHub Desktop.
Universal (AMD/Node/plain browser) module
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
/** | |
* First, better, "set exports/return" option | |
*/ | |
(function (define) { | |
//The 'id' is optional, but recommended if this is | |
//a popular web library that is used mostly in | |
//non-AMD/Node environments. However, if want | |
//to make an anonymous module, remove the 'id' | |
//below, and remove the id use in the define shim. | |
define('id', function (require) { | |
//If have dependencies, get them here | |
var a = require('a'); | |
//Return the module definition. | |
return value; | |
}); | |
}(typeof define === 'function' && define.amd ? define : function (id, factory) { | |
if (typeof module !== 'undefined' && module.exports) { | |
//Node | |
module.exports = factory(require); | |
} else { | |
//Create a global function. Only works if | |
//the code does not have dependencies, or | |
//dependencies fit the call pattern below. | |
window[id] = factory(function(value) { | |
return window[value]; | |
}); | |
} | |
})); | |
/** | |
* exports object based version, if need to make a | |
* circular dependency or need compatibility with | |
* commonjs-like environments that are not Node. | |
*/ | |
(function (define) { | |
//The 'id' is optional, but recommended if this is | |
//a popular web library that is used mostly in | |
//non-AMD/Node environments. However, if want | |
//to make an anonymous module, remove the 'id' | |
//below, and remove the id use in the define shim. | |
define('id', function (require, exports) { | |
//If have dependencies, get them here | |
var a = require('a'); | |
//Attach properties to exports. | |
exports.name = value; | |
}); | |
}(typeof define === 'function' && define.amd ? define : function (id, factory) { | |
if (typeof exports !== 'undefined') { | |
//commonjs | |
factory(require, exports); | |
} else { | |
//Create a global function. Only works if | |
//the code does not have dependencies, or | |
//dependencies fit the call pattern below. | |
factory(function(value) { | |
return window[value]; | |
}, (window[id] = {})); | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
7 years on, and UMD to consider - are there any updates (or links for referral)?