簡體   English   中英

為節點和客戶端定義模塊的最佳方法

[英]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屬性中。

http://browserify.org

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM