Created
July 23, 2015 10:20
-
-
Save chetbox/5dd0801d935a843bd3b0 to your computer and use it in GitHub Desktop.
A node module whose exports can optionally be made global
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
/* | |
This module can be required normally: | |
var optionally_global = require('./optionally_global'); | |
console.log(optionally_global.bar()); | |
Or its exports can be made global: | |
require('./optionally_global').global(); | |
console.log(bar()); | |
*/ | |
exports.foo = 'foo'; | |
exports.bar = function() { | |
return 'bar'; | |
} | |
exports.global = function() { | |
for (var cmd in exports) { | |
if (cmd !== 'global') { | |
if (!global[cmd]) { | |
global[cmd] = exports[cmd]; | |
} else { | |
console.warn('Global function "' + cmd + '" already defined'); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment