[英]Best way to define module for both node and client
我正在尝试编写一个我希望在浏览器和Node.js环境中都可用的小模块。
到目前为止,我已经提出了以下建议
(exports && window = exports)
(function(global){
// make it available to either exports OR window depending on the environment
global.Awesome = function() {
}
})(window)
这足够还是有更好的方法做到这一点? 提前致谢。
我将考虑像通常为Node那样创建模块,然后使用Browserify在客户端上也使用它。 基本上,您只需要编写脚本文件并使用'require'插入模块并在那里使用模块功能。 然后,您使用Browserify创建一个文件,该文件仅包含在html文件中的脚本标签的src属性中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.