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

前端 未结 4 1998
盖世英雄少女心
盖世英雄少女心 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:44

    Why not just wrap the entire thing with a closure and pass exports as a parameter?

    (function (exports) {
        // code here
        // ...
        exports.foo = bar;
    })(exports || this);
    

    This way it will also export it to WebWorker scope and other 'windowless' environments.

提交回复
热议问题