webpack4性能优化

【part6】webpack4及性能优化 - 图1

splitChunks 配置

webpack 总共提供了三种办法来实现 Code Splitting,如下:

  • 入口配置:entry 入口使用多个入口文件;
  • 抽取公有代码:使用 SplitChunks 抽取公有代码;
  • 动态加载 :动态加载一些代码。

这里我们姑且只讨论使用 SplitChunks 抽取公有代码。

  1. optimization: {
  2. splitChunks: {
  3. cacheGroups: {
  4. commons: {
  5. chunks: 'initial',
  6. minChunks: 2,
  7. maxInitialRequests: 5,
  8. minSize: 0,
  9. name: 'commons',
  10. },
  11. },
  12. },
  13. }

额外生成commons.js、commons.css

externals配置来提取常用库

webapck遇到此类变量名时就可以不用解析和编译至模块的内部文件中,而改用从外部变量中读取,这样能极大的提升编译速度,同时也能更好的利用CDN来实现缓存。

生产配置

contenthash

  1. output: {
  2. filename: 'scripts/[name].[contenthash:5].bundle.js',
  3. },

压缩html

  1. const minify = require('html-minifier').minify;
  2. plugins: [
  3. new CopyPlugin(
  4. [
  5. {
  6. from: join(__dirname, '../', 'src/web/components'),
  7. to: '../components',
  8. transform(content) {
  9. const resutlt = minify(content.toString('utf-8'), {
  10. collapseWhitespace: true, // 压缩空格
  11. });
  12. return resutlt;
  13. },
  14. },
  15. ],
  16. {
  17. ignore: ['*.js', '*.css', '.DS_Store'],
  18. }
  19. ),
  20. ]

压缩CSS

  1. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  2. new OptimizeCssAssetsPlugin({
  3. assetNameRegExp: /\.css$/g,
  4. cssProcessor: require('cssnano'),
  5. cssProcessorPluginOptions: {
  6. preset: ['default', { discardComments: { removeAll: true } }],
  7. },
  8. canPrint: true,
  9. }),

压缩JS, terser-webpack-plugin(代替uglifyjs-webpack-plugin)

  1. const TerserPlugin = require('terser-webpack-plugin');
  2. optimization: {
  3. minimize: true,
  4. minimizer: [
  5. new TerserPlugin({
  6. test: /\.js(\?.*)?$/i,
  7. }),
  8. ],
  9. },

打包测量

打包速度测量speed-measure-webpack-plugin

  1. const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
  2. const smp = new SpeedMeasurePlugin();
  3. module.exports = smp.wrap(merge(webpackConfig, _mergeConfig));

结果:

  1. Build completed in 1.204s
  2. SMP
  3. General output time took 1.21 secs
  4. SMP Plugins
  5. OptimizeCssAssetsWebpackPlugin took 0.28 secs
  6. TerserPlugin took 0.177 secs
  7. HardSourceWebpackPlugin took 0.067 secs
  8. HtmlWebpackPlugin took 0.034 secs
  9. CopyPlugin took 0.016 secs
  10. MiniCssExtractPlugin took 0.002 secs
  11. ProgressPlugin took 0.002 secs
  12. HtmlAfterPlugin took 0 secs
  13. SMP Loaders
  14. mini-css-extract-plugin, and
  15. css-loader, and
  16. postcss-loader took 0.425 secs
  17. module count = 1
  18. css-loader, and
  19. postcss-loader took 0.405 secs
  20. module count = 1
  21. thread-loader, and
  22. cache-loader, and
  23. babel-loader took 0.155 secs
  24. module count = 6
  25. html-webpack-plugin took 0.016 secs
  26. module count = 3
  27. modules with no loaders took 0.004 secs
  28. module count = 2

可视化打包性能分析webpack-bundle-analyzer

  1. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  2. module.exports = {
  3. plugins: [
  4. new BundleAnalyzerPlugin()
  5. ]
  6. }

加速loader, cache-loader
注意, 需要的地方使用cache-loader。 核心: 多核。

  1. yarn add --dev cache-loader
  2. {
  3. test: /\.?js$/,
  4. exclude: /(node_modules|bower_components)/,
  5. use: ['thread-loader', 'cache-loader', 'babel-loader'],
  6. },

手摸手系列 https://juejin.cn/post/6844903652956569608