Created
February 7, 2014 18:47
-
-
Save jalateras/8869090 to your computer and use it in GitHub Desktop.
How to prep a javascript module for node and browser
This file contains hidden or 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() { | |
if (typeof exports !== 'undefined') { | |
module.exports = (function() { | |
var debug = require('debug')('mymodule'); | |
return exportModule(debug); | |
})(); | |
} | |
else if (typeof define === 'function' && define.amd) { | |
define( | |
[ | |
], | |
function() { | |
return exportModule(function() {}); | |
} | |
); | |
} else { | |
window.MyModule = exportModule(function() {}); | |
} | |
function exportModule(debug) { | |
var MyModule = {}; | |
MyModule.getVersion = function() { | |
return getVersion(); | |
}; | |
return MyModule; | |
} | |
function getVersion() { | |
return '0.0.1'; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use in nodejs
var MyModule = require('mymodule')
To use in browser
<script type='text/javascript' src='mymodule.js'></script>Also supports use via requirejs.