Webpack 2 代码拆分与 System.import:依赖关系的依赖关系



好的,所以我正在努力将我们的JS正确拆分为一堆异步加载的块。

我在几个入口点中使用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 也包含moment some-other-module的块中,导致重复。

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

我将以下代码行添加到 webpack.config 的插件部分,该部分分离依赖项并根据需要并行加载它们

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

您可以在此页面上看到共享资源块插件异步的描述https://webpack.js.org/plugins/commons-chunk-plugin/

最新更新