optimización webpack4 análisis que consume mucho tiempo complemento de plugin de paquete web de medición de velocidad

能够列出输出、loader和plugins的耗时,过长将会使用黄色和红色显示
1、下载
	cnpm install -D speed-measure-webpack-plugin
	
2、使用
	const SpeedMeasurePlugin=require('speed-measure-webpack-plugin')
	
	const smp = new SpeedMeasurePlugin();
	smp.wrap({配置config})

Imagen de efecto:
Inserte la descripción de la imagen aquí
Ejemplo de código:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {
    
     CleanWebpackPlugin } = require('clean-webpack-plugin');
const HTMLInlineCSSWebpackPlugin = require ( "html-inline-css-webpack-plugin" ). default ;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin')
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const glob = require('glob');
const fs = require('fs');
const TerserPlugin = require("terser-webpack-plugin");
const SpeedMeasurePlugin=require('speed-measure-webpack-plugin')

const smp = new SpeedMeasurePlugin();

const setOptions = ()=>{
    
    
  const entry = {
    
    };
  const htmlWebpackPlugin = [];

  const entryFiles = glob.sync(path.resolve(__dirname, './src/*/index.js'))
  const fileReg = /src\/(.*)\/index\.js/;

  entryFiles.forEach((filePath, index) => {
    
    
    const fileName = filePath.match(fileReg);
    
    entry[fileName[1]] = filePath;
    htmlWebpackPlugin.push(
      new HtmlWebpackPlugin({
    
    
        template: `./src/${
      
      fileName[1]}/index.html`,
        filename:fileName[1]+'.html',
        chunks:[fileName[1]]
      })
    )
  })

  return {
    
    
    entry,
    htmlWebpackPlugin
  }
}

module.exports =smp.wrap( {
    
    
  entry: setOptions().entry,
  output: {
    
    
    path: path.resolve(__dirname, 'dist'),
    filename: '[name]_[chunkhash:8].js'
  },
  module: {
    
    
    rules: [
      {
    
    
        test: /\.js$/,
        use:['babel-loader']
      },
      {
    
    
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
    
    
            loader: "postcss-loader",
            options: {
    
    
              postcssOptions: {
    
    
                plugins: [
                  [
                    "autoprefixer",
                    {
    
    
                      overrideBrowserslist: [
                          "Android 4.1",
                          "iOS 7.1",
                          "Chrome > 31",
                          "ff > 31",
                          "ie >= 8",
                          "> 1%", // 必须大于 1% 用户使用的浏览器
                          //'last 2 versions', // 所有主流浏览器最近的 2个版本
                      ],
                    },
                  ],
                ],
              },
            },
          },
          {
    
    
            loader: 'px2rem-loader',
            options: {
    
    
              remUnit: 75, //初始稿的10分之一,代表设计稿为750,1rem=75px,
              remPrecision:8,  //精确的小数点位数
            }
          }
        ]
      },
      {
    
    
        test: /\.txt$/i,
        use: [
          {
    
    
            loader: 'raw-loader',
            options: {
    
    
              esModule: false,
            },
          },
        ],
      }
    ]
  },
  optimization: {
    
    
    splitChunks: {
    
    
      cacheGroups: {
    
    
        commons: {
    
    
          test: /(react)|(react-dom)/,
          chunks: 'all',
          name: 'hhh',
          
        }
      }
    }
  },
  mode: "production",
  plugins: [
    ...setOptions().htmlWebpackPlugin,
    new MiniCssExtractPlugin({
    
     filename: 'css/build.css' }),
    new HTMLInlineCSSWebpackPlugin(),
    // new FriendlyErrorsWebpackPlugin(),
    new CleanWebpackPlugin(),
    function () {
    
    
      this.hooks.done.tap("done", (stats) => {
    
    
        if (stats.compilation.errors && stats.compilation.errors.length && process.argv.indexOf('--watch') == -1) {
    
    
          console.log('build error');
          process.exit(1);
        }
      })
    }
  ]
})

Supongo que te gusta

Origin blog.csdn.net/weixin_43294560/article/details/113485178
Recomendado
Clasificación