我使用webpack逗号来定义将与其他生成的包共同使用的全局库对象(公共组件)。
(**components.js**)通用代码
MyLib = window.MayLib = {}
MyLib.Utils = {
commonFn : function (){
/* common code */
}
}
module.exports = MayLib;(**dashboard.js**)第一次常用的
require.ensure ('./components', function () {
MyLib.Dashboard = {
/** Dashboad code here */
}
})(**account.js**) 第二种常用用法
require.ensure ('./components', function (){
MyLib.Account = {
/** Account code here */
}
})在生成包之后,已经创建了一个新的公共代码,但是MyLib is undefined in global window, "cannot set property of undefined"
发布于 2015-10-14 09:35:40
尽管我认为您可以通过要求回调中的MyLib对象来轻松地解决问题。不用再把它暴露在全局范围内了。
require.ensure ('./components', function (require){
var MyLib = require('./components');
MyLib.Account = {
/** Account code here */
}
})Sidenote:您可以尝试通过使用CommonsChunkPlugin来简化您的代码分裂,然后只使用简单的require('components'),其余的就由插件来完成。
https://stackoverflow.com/questions/33075763
复制相似问题