Compressing the size of the packaged project and improving the packaging speed are very important links in the front-end performance optimization. I combine the practice summary in the work and sort out some conventional and effective performance optimization suggestions.
Background of the project
- Technology stack: vue-cli3 + vue2 + webpack4
- Main plug-ins: elementUI + echarts + axios + momentjs
- Goal: Through a series of optimization schemes, compare the changes in packaging volume and speed before and after to verify the effectiveness of the scheme.
Project initial size and velocity
- Initial volume: 2.25M
The vue project can add the –report command: “build”: “vue-cli-service build –report”, the dist directory will generate a report.html file after packaging, which is used to analyze the size of each file.
Or analyze by installing webpack-bundle-analyzer. The steps are as follows:
1) Install
1 | $ npm install webpack-bundle-analyzer -D |
2) Introduced in vue.config.js
1 | const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; |
3) After running npm run serve, you can see the analysis page by openning http://127.0.0.1:xxxx in the browser.
Start optimizing ✈︎
1. Externals extracts project dependencies
As can be seen from the above packaging analysis page, chunk-vendors.js the volume is 2.21M, and the largest files are some public dependency packages, so as long as these dependencies are extracted, the problem of chunk-vendors.js being too large can be solved.
You can use externals to extract these dependent packages, tell webpack that these dependencies are provided by the external environment, you can ignore them when packaging, and they will not be typed into chunk-vendors.js.
1) Configuration in vue.config.js:
1 | module.exports = { |
2) Use CDN to introduce dependencies in index.html
1 | <body> |
Verify the validity of externals:
Repackage, the latest data is as follows:
Packing volume: 1.12M
Packing speed: 18879ms
After using externals, the package size is compressed by 50%, and the packaging speed is increased by 26%.
2. On-demand introduction of component library
Why not use externals to handle component libraries?
Disadvantages of externals: imported directly in html, loses the function of importing on demand, and can only import the complete js and css of the component library.
The principle of on-demand introduction of component libraries: only the specified components and corresponding styles are finally introduced.
Element-UI needs to be implemented with the help of the babel-plugin-componentplugin. The functions of the plugin are as follows:
To import the Button component as needed:
1 | import { Button } from 'element-ui' |
Compiled file (automatically import button.css):
1 | import _Button from "element-ui/lib/button"; |
Through this plugin, only the specified components and styles are finally introduced to reduce the size of the component library.
1) Install babel-plugin-component
1 | $ npm install babel-plugin-component -D |
2) Introduced in babel.config.js
1 | module.exports = { |
Verify the validity of the on-demand import of the component library:
Repackage, the latest data is as follows:
- Packing volume: 648KB
- Packing speed: 15135ms
After the component library is introduced on demand, the package volume is reduced by 72%, and the packaging speed is increased by 40%.
At the same chunk-vendors.css time the volume has also been significantly reduced, from 206KB down to 82KB.
3. Reduce the volume of tripartite dependencies
Continue to analyze the packaged file, momentjs is used in the project, and it is found that there are many unused language packs after packaging.
Use moment-locales-webpack-plugin to remove useless language packs.
1) Install
1 | npm install moment-locales-webpack-plugin -D |
2) Introduced in vue.config.js
1 | const MomentLocalesPlugin = require('moment-locales-webpack-plugin'); |
Verify the validity of the plugin:
Repackage, the latest data is as follows:
Packing volume: 407KB
Packing speed: 10505ms
After reducing the size of the three-party dependencies, the package volume is reduced by 82%, and the packaging speed is increased by 59%.
4. HappyPack multi-thread packaging
Since webpack running on Node.js is a single-threaded model, we need webpack to be able to process multiple tasks at the same time and take advantage of the power of multi-core CPU computers.
HappyPack Multi-thread packaging can be realized. It decomposes tasks to multiple sub-processes for concurrent execution. After the sub-processes are processed, the results are sent to the main process to increase the packaging speed.
1) Install
1 | npm install HappyPack -D |
2) Introduced in vue.config.js
1 | const HappyPack = require('happypack'); |
Verify the validity of HappyPack:
Repackage, the latest data is as follows:
Packing speed: 8949ms
After using HappyPack, the packaging speed has been further increased by 65%.
Since the test project is small, the packaging time is not shortened too much. The actual measurement found that the more complex the project, the more obvious the improvement of the packaging speed of HappyPack.
5. Gzip compression
Online projects are generally combined with the construction tool webpack plug-in or server-side configuration nginx to achieve gzip compression for http transmission. The purpose is to minimize the size of the server-side response file and optimize the return speed.
html, js, css resources, after using gzip, the volume can usually be compressed by more than 70%.
Here is how to use webpack for gzip compression, using compression-webpack-plugin
1) install
1 | npm install compression-webpack-plugin -D |
2) Introduced in vue.config.js
1 | const CompressionPlugin = require('compression-webpack-plugin'); |
Verify the validity of the plugin:
Repackage, 407KB the original volume is compressed to 108KB.
6. DllPlugin dynamic link library
DllPlugin similar to the role of externals, they both pull out dependencies and save packaging time. The difference is that DllPlugin packages the dependencies separately, so that only the business code is built each time in the future, while externals is imported by converting dependencies into CDN.
When the company does not have good CDN resources or does not support CDN, you can consider using DllPlugin to replace externals.
The DllPlugin configuration process is roughly divided into three steps:
1) Create dll.config.js configuration file
1 | import { DllPlugin } from "webpack"; |
2) package.json configuration script
1 | "build:dll": "webpack --config ./dll.config.js", |
3) Use to DllReferencePlugin reference the dll file generated by packaging to the required precompiled dependencies, and html-webpack-tags-plugin automatically insert the dll file when packaging.
vue.config.js is configured as follows
1 | import { DllReferencePlugin } from "webpack"; |
Run npm run build:dll to package and generate dependency files, and then just run npm run build to build business code.
Optimization summary
After a series of optimizations above, we can see:
The package size 2.25M is reduced 407KB to 82% compressed.
The packaging speed 25386ms is reduced to 8949ms, which is increased by 65%.
Although these methods are very conventional, they can effectively improve the performance of the project.
This article mainly introduces the optimization method of project packaging and construction. The next article will talk about performance optimization at the business code level, and there will be many interesting solutions.