Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
recipe to expose a JS module for CommonJS, AMD and global namespace
(function(context) {
'use strict';
var modName = 'stuff'; // used only if mod is a function and no CommonJS or AMD supported
// define your module here... can be a function or an object
var mod = {};
// export
if (typeof module === 'object' && module.exports) { // CommonJS
module.exports = exports = mod;
}
else if (typeof define === 'function' && define.amd) { // AMD
define(function() {
return mod;
});
}
else { // at this stage context is window
if (typeof mod === 'function') {
context[modName] = mod;
}
else {
for (var k in mod) {
context[k] = mod[k];
}
}
}
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment