Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active February 11, 2024 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/9540207 to your computer and use it in GitHub Desktop.
Save JosePedroDias/9540207 to your computer and use it in GitHub Desktop.
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