将全局对象转换为要在Webpack中使用的包

时间:2017-12-19 22:33:07

标签: javascript webpack

我有多个文件,如:

  • library0.js
  • library1.js
  • ...
  • libraryn.js

每个填充全局对象“MY_GLOBAL”的一部分,如下例所示:

library0.js

// check that the MY_GLOBAL namespace exists
if (typeof(MY_GLOBAL) === 'undefined') {
    var MY_GLOBAL = {};
}

MY_GLOBAL.TIMEOUT =  120000; // 2 minutes

MY_GLOBAL.extractErrMsg = function(reqResponse) {
    console.log(reqResponse);
};

library1.js

// check that the MY_GLOBAL namespace exists
if (typeof(MY_GLOBAL) === 'undefined') {
    var MY_GLOBAL = {};
}

MY_GLOBAL.STRING =  'STRING';

MY_GLOBAL.exposed = function() {
    console.log("This is exposed);
};

目前在我的index.html文件中,我在脚本包含中添加了所有这些文件。因此我所有其他js文件都可以简单地调用:

MY_GLOBAL.extractErrMsg

或MY_GLOBAL上的任何其他功能/对象。

我正在转向WebPack构建,我希望将所有这些文件作为捆绑包并将其全局导入,这样我所有的文件都可以像现在一样使用它。将我的库文件转换为捆绑包的方法是什么,然后可以在WebPack中全局导入,这样我就不需要修改每个其他文件来导入MY_GLOBAL?

谢谢!

0 个答案:

没有答案