简体   繁体   English

使用Webpack将供应商库分成多个块

[英]Split vendor libraries into multiple chunks with webpack

I'd like to split my vendor code into two chunks, one that contains all angular libraries, and another that contains everything else. 我想将供应商代码分成两部分,一个包含所有角度库,另一个包含其他所有内容。

My angular app has a single entry point and is setup something like: 我的角度应用程序只有一个入口点,并且设置如下:

entry: {
    app: './path_to/app.js',
    vendor: ['jquery', 'moment', 'numeral'],
    'vendor.angular': ['angular', 'angular-route', 'angular-numeraljs']
}

I then use the CommonsChunkPlugin to configure the two other bundles: 然后,我使用CommonsChunkPlugin配置其他两个捆绑包:

new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor',
    chunks: ['app'],
    warnings: false,
    filename: 'vendor.bundle.js'
})
new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor.angular',
    chunks: ['app'],
    warnings: false,
    filename: 'vendor.angular.bundle.js'
})

This generates 3 files: 这将生成3个文件:

Version: webpack 1.13.1
Time: 12719ms
                   Asset     Size  Chunks             Chunk Names
           app.bundle.js  19.2 kB       0  [emitted]  app
        vendor.bundle.js   484 kB       1  [emitted]  vendor
vendor.angular.bundle.js   652 kB       2  [emitted]  vendor.angular
   [0] multi vendor.angular 124 bytes {2} [built]
   [0] multi vendor 88 bytes {1} [built]
    + 124 hidden modules

app.bundle.js contains just my app code. app.bundle.js仅包含我的应用程序代码。
vendor.bundle.js contains all 3rd party libs excluding angular stuff vendor.bundle.js包含所有第三方库,不包括有角度的东西
vendor.angular.bundle.js contains all angular stuff AND all my 3rd party libs that are already inside of vendor.bundle.js. vendor.angular.bundle.js包含所有角度内容以及我已经在vendor.bundle.js中的所有第3方库。

Is there anyway to have JUST the angular modules bundled in vendor.angular.bundle.js, without automatically including the other 3rd party libs? 无论如何,有没有将角度模块捆绑在vendor.angular.bundle.js中,而不会自动包括其他第三方库?

Figured this out: 想通了:

The order of the CommonsChunkPlugin's matter in the plugins array. plugins数组中CommonsChunkPlugin的问题的顺序。

To get the desired 'chunking', here's the change I had to make: 为了获得理想的“分块”,这是我必须进行的更改:

  1. Re-order the CommonsChunkPlugins so that the angular chunk was first. 重新排序CommonsChunkPlugins,以便有角度的块是第一个。
  2. Update the 'vendor' config below to use 'vendor.angular' in the 'chunks' array. 更新下面的“ vendor”配置,以在“块”数组中使用“ vendor.angular”。

The updated CommonsChunkPlugins now looks like: 更新后的CommonsChunkPlugins现在看起来像:

new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor.angular',
    chunks: ['app'],
    warnings: false,
    filename: 'vendor.angular.bundle.js'
})
new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor',
    chunks: ['vendor.angular'],
    warnings: false,
    filename: 'vendor.bundle.js'
})

The above now yields: 现在上面产生:

Version: webpack 1.13.1
Time: 7451ms
                   Asset     Size  Chunks             Chunk Names
           app.bundle.js  19.2 kB       0  [emitted]  app
        vendor.bundle.js   484 kB       1  [emitted]  vendor
vendor.angular.bundle.js   221 kB       2  [emitted]  vendor.angular
   [0] multi vendor.angular 124 bytes {2} [built]
   [0] multi vendor 88 bytes {1} [built]
    + 124 hidden modules

Running: 正在运行:

webpack --progress --display-modules --display-chunks -v

I'm able to verify that all angular related modules are now in the vendor.angular.bundle.js, and all non-angular modules are indeed in vendor.bundle.js 我可以验证所有与角度相关的模块现在都在vendor.angular.bundle.js中,并且所有非角度模块确实都在vendor.bundle.js中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM