简体   繁体   English

为节点和客户端定义模块的最佳方法

[英]Best way to define module for both node and client

I'm trying to write a small module that I want to be available both on the Browser and Node.js environment. 我正在尝试编写一个我希望在浏览器和Node.js环境中都可用的小模块。

So far I've come up with the following 到目前为止,我已经提出了以下建议

(exports && window = exports)

(function(global){

    // make it available to either exports OR window depending on the environment
    global.Awesome = function() {

    }

})(window)

is that sufficient or is there any better way to do this? 这足够还是有更好的方法做到这一点? Thanks in advance. 提前致谢。

I would consider just creating your module as you normally would for Node and then using Browserify to use it on the client as well. 我将考虑像通常为Node那样创建模块,然后使用Browserify在客户端上也使用它。 Basically you just write a script file and use 'require' to pull in your module and make use of your module functions right there. 基本上,您只需要编写脚本文件并使用'require'插入模块并在那里使用模块功能。 Then you use Browserify to create a single file that you just include in the src attribute of a script tag in your html file. 然后,您使用Browserify创建一个文件,该文件仅包含在html文件中的脚本标签的src属性中。

http://browserify.org http://browserify.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM