Last active
February 11, 2024 23:06
-
-
Save JosePedroDias/9540207 to your computer and use it in GitHub Desktop.
recipe to expose a JS module for CommonJS, AMD and global namespace
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
(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