Last active
February 12, 2024 00:05
-
-
Save miguelmota/9152810 to your computer and use it in GitHub Desktop.
Reusable module for Node.js and browser
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(root) { | |
var fglobal = typeof global === 'object' && global; | |
if (fglobal.global === fglobal || fglobal.window === fglobal) { | |
root = fglobal; | |
} | |
if (typeof define === 'function' && typeof define.amd === 'object') { | |
define([], function() { | |
return mymodule; | |
}); | |
} else if (typeof exports === 'object') { | |
if (typeof module === 'object' && module.exports === 'object') { | |
module.exports = mymodule; | |
} else { | |
exports.mymodule = mymodule; | |
} | |
} else { | |
root.mymodule = mymodule; | |
} | |
}(this)); |
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
<!-- Browser --> | |
<script src="mymodule.js"></script> | |
<script> | |
MyModule.foo // 'bar' | |
</script> |
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
// Node.js | |
var MyModule = require('mymodule'); | |
MyModule.foo // 'bar' |
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(global) { | |
var MyModule = (function() { | |
return { | |
foo: 'bar' | |
}; | |
})(); | |
if (typeof exports !== 'undefined') { | |
if (typeof module !== 'undefined' && module.exports) { | |
exports = module.exports = MyModule; | |
} | |
exports.MyModule = MyModule; | |
} else if (typeof define === 'function' && define.amd) { | |
define([], function() { | |
return MyModule; | |
}); | |
} else { | |
global.MyModule = MyModule; | |
} | |
})(window); |
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(exports){ | |
exports.myModulet = function(){ | |
return 'hello world'; | |
}; | |
})(typeof exports === 'undefined'? this['myModule']={}: exports); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment