I'm considering adopting browserify for some of my projects, but would like to make sure that others don't have to use browserify if they want to use the (bundled) code. The obvious way to do this is to both expose the modules exports through module.exports
as well as through a window.
global. However, I'd rather not pollute the global namespace for those who are require
ing the script.
Is it possible to detect if a script is being require
d? If it is, then I could do something like:
var mymodule = (function() { ... })();
if (isRequired()) {
module.exports = mymodule;
} else {
window.mymodule = mymodule;
}
Note that no matter what, this will be bundled beforehand, so the var mymodule
won't be exposing a global. Also, currently I'm using the revealing module pattern, but would be willing to switch to something more appropriate for browserify.
What's the best way to make a module both require
able and <script src=
able? Is it best to just expose a global in both circumstances?
question from:
https://stackoverflow.com/questions/16172035/browserify-use-module-exports-if-required-otherwise-expose-global 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…