webpack4.0 some tricks when packing.

Source: Internet
Author: User

The Webpack of 4.0 is not a little bit stronger than the previous version.

I did a lot of optimizations when I was packing.

And say some interesting tricks that have been discovered during this time.

1. The packaging of the file is very large, do not know what the reason can be used Webpack-bundle-analyzer

Very good analysis tools, you can know that the packaging of the files are from where, each of how to use the method is very simple 1. Introduced 2. Put it in the plugin.
Const Bundleanalyzerplugin = require ('webpack-bundle-analyzer'). Bundleanalyzerplugin;
Config.plugins.push (new  bundleanalyzerplugin ())

Then the page will pop up in the browser after packing.

Like I'm telling me this is highlightjs too big, more than 700 KB This solution is generally a way to get away from a part highlight

This thing is very cool, very convenient, highly recommended

2. Public module separation

optimization: {splitchunks: {cachegroups: {commons}: {chunks: ' /c0>'Initial', Minchunks:2, Maxinitialrequests:5, MinSize:0}, Vendor: {//extract the third-party moduleTest:/node_modules/, chunks:'Initial', Name:'Vendor', Priority:Ten,//PriorityEnforce:true                }            }        }    }

Webpack4.0 of the split has become more humane, can be very convenient to node_modules public functions out, pull out will not be wrong, the client cache up, this part is not changed

3. Compression Code Compression-webpack-plugin

The use of mode=production itself is compressed, but this compression is only code compression, and the browser is actually supported Gzip and other various formats of compressed files, we can give the browser a compressed package

Compressing into gzip with this plugin will make the code one or two times times smaller. It's easy to use.

Const Compressionwebpackplugin = require ('compression-webpack-plugin')

 config.plugins.push ( new   Compressionwebpackplugin ({asset:   " [path].gz[query]  "    " gzip   '  

Of course, the premise of this thing is to have a server support.

The general practice is that when we request a JS resource, the configuration is returned as a compressed package instead of JS

Just introduce these three, if your webpack has not used these three kinds of optimization, then try it quickly.

webpack4.0 some tricks when packing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.