1

好的,所以我正在努力将我们的 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,使 Webpackmoment也包含在some-other-module's 块中,导致重复。

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

4

1 回答 1

0

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

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

您可以在此页面上看到 CommonsChunkPlugin async 的描述 https://webpack.js.org/plugins/commons-chunk-plugin/

于 2017-02-17T15:34:40.280 回答