Browserify: Use module.exports if required, otherwise expose global

前端 未结 4 2071
盖世英雄少女心
盖世英雄少女心 2021-01-31 08:09

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 o

4条回答
  •  情书的邮戳
    2021-01-31 08:41

    Assuming another library hasn't created a global module.exports object, you can simply check for the existence of module.exports

    var mymodule = (function() { ... })();
    if (module && module.exports) {
      module.exports = mymodule;
    } else {
      window.mymodule = mymodule;
    }
    

提交回复
热议问题