简体   繁体   English

基于webpack路由的代码拆分减少了我的包大小,但增加了我的块大小

[英]webpack route based code splitting reduced my bundle size but increased my chunk size

My bundle size was around 2MB uncompressed and 400kb compressed 我的捆绑包大小约为2MB(未压缩)和400kb(压缩)

在此处输入图片说明

在此处输入图片说明

This bundle was created using webpack 1.15.0 . 该捆绑包是使用webpack 1.15.0创建的。

And the film strip is like this 电影地带就是这样 在此处输入图片说明

This is the Webpackanalyzer output 这是Webpackanalyzer的输出 在此处输入图片说明

Then i have updated webpack to 2.6.1 and enabled code spliting and moved all third party js to vendor.js, Now bundle.js was containing only my app's code. 然后,我将webpack更新到2.6.1,并启用了代码拆分功能,并将所有第三方js移至vendor.js,现在bundle.js仅包含我应用程序的代码。

My bundle size is like this 我的捆绑包尺寸是这样的 在此处输入图片说明

This is the uncompressed size. 这是未压缩的大小。

But the load time increased to 7.09s 但是加载时间增加到了7.09s 在此处输入图片说明

Then we tried chunking based on routes . 然后我们尝试基于路由进行分块 using require.ensure . 使用require.ensure We thought the bundle which 1.75mb will be broken into small chunks based on routes. 我们认为,根据路线,将1.75mb的捆扎成小块。 But each route's chunk was much bigger than expected. 但是每条路线的区块比预期的要大得多。 And the sum of the total routes chunk is more than the bundle.js size 并且总路由块的总和大于bundle.js的大小

Here is the final result after enabling route based chunking 这是启用基于路由的分块后的最终结果 在此处输入图片说明

As you can see bundle.js is reduced from 1.75MB to 180kb . 如您所见,bundle.js从1.75MB减少到180kb

As I can see from WebpackBundle Analyzer , Each chunk has its node_modules inside each chunk. 从WebpackBundle Analyzer中可以看到,每个块在每个块中都有其node_modules。 This node modules are same for all the chunks. 所有块的此节点模块均相同。

在此处输入图片说明

This is the comparision of two routes. 这是两条路线的比较。 在此处输入图片说明 .

Is there any way to reduce the chunk size of each route ? 有什么办法可以减少每个路由的块大小?

I am using CommonChunkPlugin. 我正在使用CommonChunkPlugin。

new CommonsChunkPlugin({
            name: 'common',
            filename: 'commons-[hash:8].js',
             chunks: ['vendor'],
             minChunks: Infinity
        }),
new webpack.optimize.CommonsChunkPlugin({ name: 'meta', chunks: ['vendor'], filename: 'meta.[hash].js' }),

As @user1471177 mentioned. 正如@ user1471177所提到的。 I have add one more CommonChunkPlugin which makes common code of all the route's chunks in to one common chunk. 我再添加一个CommonChunkPlugin,它将所有路由块的通用代码合并为一个通用块。

new CommonsChunkPlugin({
        name: 'common',
        filename: 'commons-[hash:8].js',
         chunks: ['vendor'],
         minChunks: Infinity
    }),
new webpack.optimize.CommonsChunkPlugin({ name: 'meta', chunks: ['vendor'], filename: 'meta.[hash].js' }),
new webpack.optimize.CommonsChunkPlugin({ name: 'common', chunks: ['0','1','2',.......'30'], filename: 'common.[hash].js' , minChunks: 2}),

Which seperated all the common code in to one common.js chunk. 它将所有通用代码分成一个common.js块。

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

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