2017-01-18 137 views
1

好吧,所以我正在正确分割我们的JS在一堆异步加载的块。Webpack 2代码分裂与System.import:依赖关系的依赖

我在几个入口点使用import,那伟大工程:

module.exports = Promise.all([ 
    import('moment'), 
    import('some-other-module') 
]).then((deps) => { 
    let [moment, someOtherModule] = deps; 
} 

和其他地方:

module.exports = Promise.all([ 
    import('moment'), 
]).then((deps) => { 
    let [moment] = deps; 
} 

的WebPack成功创建momentsome-other-module 单独的块,并加载文件异步需要的时候。

但是:

some-other-module实际上需要moment为好,使的WebPack包括momentsome-other-module的块,导致重复。

这是预期的行为?如果是这样,那么推荐的解决方案是什么?

回答

0

我添加的代码,以我的插件webpack.config的部分如下线,如并行

new webpack.optimize.CommonsChunkPlugin({ 
    async: true 
}) 

需要,您可以看到此页面上CommonsChunkPlugin异步的描述分离的依赖关系并加载它们 https://webpack.js.org/plugins/commons-chunk-plugin/