webpack 的异步组件 生成commonchunks
1 new webpack.optimize.CommonsChunkPlugin({ 3 async: 'async-common', 4 minChunks: function (module, count) { 5 // any required modules inside node_modules are extracted to vendor 6 return ( 7 count >= 2 8 ) 9 } 10 })
上述代码是将异步组件的公共组件打包到async-common中,但是build后通过 Webpack Bundle Analyzer 查看,发现并无效果.
正确配置如下:
new webpack.optimize.CommonsChunkPlugin({ names: Object.keys(utils.getEntries()), async: 'async-common', minChunks: function (module, count) { // any required modules inside node_modules are extracted to vendor return ( count >= 2 ) } })
如上边代码,需要添加name或者names,此选项为entry的名字,因为我是多页面且多个入口,所以使用names, 通过Object.keys(utils.getEntries()) 获取我全部的入口名