Webpack 2 跨包复制模块?

Webpack 2 duplicating modules across bundles?

我是 Webpack 2 的新手。创建包时,一些模块会跨包重复。这是我的 webpack 配置:

module.exports = {
    node: {
      fs: "empty"
    },
    context: __dirname,
    entry: {
        "vendor": "./src/vendor-bundle-config.ts",
        "app" : "./src/app/app.module"
    },  
    output: {
        filename: '[name].js',
        path: './'
    }   
}

和我的供应商捆绑配置:

// Angular
import '@angular/platform-browser';
import '@angular/platform-browser-dynamic';
import '@angular/core';
import '@angular/common';
import '@angular/http';
import '@angular/router';

// RxJS
import 'rxjs';

无论我做什么,rxjs(也许还有其他模块,我还没有进一步检查)在两个包中都是重复的。奇怪的是,我正在用一个非常基本的 Angular 项目来测试这一切——它有 AppComponent,仅此而已。目前唯一引用 rxjs 的地方是 package.jsonvendor-bundle-config

我已经尝试配置 CommonsChunkPlugin,但它似乎没有做任何事情。我不确定这是否是我应该进一步研究的问题。

编辑:这是我能记得的最好的 CommonsChunkPlugin 配置:

new webpack.optimize.CommonsChunkPlugin({
  name: "commons",
  filename: "commons.js",
})

这是来自 webpack 文档示例。

我missing/doing哪里错了?

谢谢。

插件的name选项是要与entries.So分开的chunk的名字,改成vendor。有关详细信息,请参阅 code splitting

想通了。我的 CommonsChunkPlugin 配置中缺少 minChunks 选项。它需要看起来像这样才能工作:

new webpack.optimize.CommonsChunkPlugin({
  name: "commons",
  filename: "commons.js",
  minChunks: 2     <-- this is what I was missing
})