【前端优化】使用speed-measure-webpack-plugin分析前端运行、打包耗时,优化项目

news2025/6/3 3:18:23

记录下

项目安装speed-measure-webpack-plugin

打包分析:

修改vue.config.js文件
speed-measure-webpack-plugin进行构建速度分析,需要包裹整个 configureWebpack 配置

const originalConfig = {
  publicPath: '/',
  assetsDir: 'assets',
  parallel: true,
  devServer: {
    hot: true
  },
  lintOnSave: false,
  runtimeCompiler: true,
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@views', resolve('src/views'))
      .set('@comp', resolve('src/components'))
      .set('@root', resolve('/'))
    config.module
      .rule('images')
      .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
      .type('asset')
      .parser({
        dataUrlCondition: {
          maxSize: 8 * 1024 // 8KB以下转base64 //2560
        }
      })
    //   .set('generator', {
    //     filename: 'img/[name].[hash:8][ext]'
    //   })
    config.module
      .rule('images')
      .use('image-webpack-loader')
      .loader('image-webpack-loader')
      .options({
        bypassOnDebug: true
      })
      .end()
  },
  configureWebpack: {
    cache: {
      type: 'filesystem', // 使用文件缓存
      buildDependencies: {
        config: [__filename] // 缓存依赖
      },
      allowCollectingMemory: true,
      maxMemoryGenerations: 1
    },
    optimization: {
      minimizer: minimizer,
      removeEmptyChunks: process.env.NODE_ENV === 'production',
      splitChunks: splitChunks
    },
    plugins: plugins,
    module: {
      noParse: /jquery/,
      rules: [
        {
          test: /\.js$/,
          include: path.resolve(__dirname, 'src'),
          exclude: /node_modules/,
          use: [
            {
              loader: 'thread-loader',
              options: {
                workers: cpuCount,
                workerParallelJobs: 20,
                workerNodeArgs: ['--max-old-space-size=1024'] // 限制子进程内存
                // poolTimeout: 2000 // 空闲时自动关闭
              }
            },
            {
              loader: 'babel-loader',
              options: {
                babelrc: true,
                cacheDirectory: true
              }
            }
          ]
        }
      ]
    }
  },
  css: {
    loaderOptions: {
      // You need to configure a global variable if you want to use it in a component
      scss: {
        additionalData: '@import "~@/assets/css/variables.scss";'
      }
    }
  },
  // 设为false打包时不生成.map文件
  productionSourceMap: false
}
module.exports = {
  ...originalConfig,
  configureWebpack: smp.wrap(originalConfig.configureWebpack)
}

npm run dev运行,获取分析:

SMP  ⏱
General output time took 7 mins, 42.001 secs

 SMP  ⏱  Plugins
DefinePlugin took 0 secs
TerserPlugin took 0 secs

 SMP  ⏱  Loaders
@vue/vue-loader-v15, and
mini-css-extract-plugin, and
css-loader, and
postcss-loader, and
sass-loader, and
@vue/vue-loader-v15 took 4 mins, 58.75 secs
  module count = 2340
css-loader, and
@vue/vue-loader-v15, and
postcss-loader, and
sass-loader, and
@vue/vue-loader-v15 took 4 mins, 53.93 secs
  module count = 1170
@vue/vue-loader-v15, and
thread-loader, and
babel-loader, and
thread-loader, and
babel-loader, and
@vue/vue-loader-v15 took 3 mins, 22.91 secs
  module count = 3430
mini-css-extract-plugin, and
css-loader, and
postcss-loader took 2 mins, 41.67 secs
  module count = 21
thread-loader, and
babel-loader, and
thread-loader, and
babel-loader took 2 mins, 33.61 secs
  module count = 404
mini-css-extract-plugin, and
css-loader, and
postcss-loader, and
sass-loader took 2 mins, 12.24 secs
  module count = 4
modules with no loaders took 2 mins, 2.27 secs
  module count = 2244
image-webpack-loader took 1 min, 36.096 secs
  module count = 327
@vue/vue-loader-v15 took 1 min, 31.51 secs
  module count = 5127
css-loader, and
postcss-loader took 1 min, 13.14 secs
  module count = 21
css-loader, and
postcss-loader, and
sass-loader took 50.62 secs
  module count = 4
@vue/vue-loader-v15, and
mini-css-extract-plugin, and
css-loader, and
postcss-loader, and
@vue/vue-loader-v15 took 49.91 secs
  module count = 6
css-loader, and
@vue/vue-loader-v15, and
postcss-loader, and
@vue/vue-loader-v15 took 15.11 secs
  module count = 3
html-webpack-plugin took 1.9 secs
  module count = 1
raw-loader took 0.819 secs
  module count = 1
script-loader took 0.032 secs
  module count = 1



修改配置

1.图片处理仅在生产环境使用

    if (process.env.NODE_ENV === 'production') {
      config.module
        .rule('images')
        .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
        .type('asset')
        .parser({
          dataUrlCondition: {
            maxSize: 8 * 1024 // 8KB以下转base64 //2560
          }
        })
      //   .set('generator', {
      //     filename: 'img/[name].[hash:8][ext]'
      //   })
      config.module
        .rule('images')
        .use('image-webpack-loader')
        .loader('image-webpack-loader')
        .options({
          bypassOnDebug: true
        })
        .end()
    } else {
      config.module.rule('images').uses.delete('image-webpack-loader') // 移除图像压缩
    }
  1. scss设置
  css: {
    loaderOptions: {
      // You need to configure a global variable if you want to use it in a component
      scss: {
        implementation: require('sass'),
        additionalData: '@import "~@/assets/css/variables.scss";'
      }
    }
  },
  1. 改用esbuild-loader,用了之后是要快一点,但这个速度感觉还是不对啊
// 删除原有的babel-loader配置
    config.module.rules.delete('js')

    // 添加esbuild-loader
    config.module
      .rule('js')
      .test(/\.(js|mjs|jsx)$/)
      // .exclude.add(/node_modules/)
      // .end()
      .use('esbuild-loader')
      .loader('esbuild-loader')
      .options({
        target: 'es2015',
        loader: 'jsx'
      })
thread-loader, and 
babel-loader took 1 min, 23.34 secs
  module count = 374
-------
esbuild-loader took 52.51 secs
  module count = 395

再次优化后,打包速度提升:

 SMP  ⏱  
General output time took 4 mins, 5.18 secs

 SMP  ⏱  Plugins
DefinePlugin took 0.002 secs

 SMP  ⏱  Loaders
css-loader, and 
@vue/vue-loader-v15, and 
postcss-loader, and 
sass-loader, and 
@vue/vue-loader-v15 took 3 mins, 16.68 secs
  module count = 1170
@vue/vue-loader-v15 took 2 mins, 11.74 secs
  module count = 5127
@vue/vue-loader-v15, and 
esbuild-loader, and 
@vue/vue-loader-v15 took 1 min, 50.85 secs
  module count = 3430
modules with no loaders took 1 min, 27.56 secs
  module count = 2609
css-loader, and 
postcss-loader took 1 min, 11.28 secs
  module count = 21
css-loader, and 
postcss-loader, and 
sass-loader took 1 min, 8.42 secs
  module count = 4
esbuild-loader took 52.51 secs
  module count = 395
css-loader, and 
@vue/vue-loader-v15, and 
postcss-loader, and 
@vue/vue-loader-v15 took 8.85 secs
  module count = 3
@vue/vue-loader-v15, and 
vue-style-loader, and 
css-loader, and 
postcss-loader, and 
sass-loader, and 
@vue/vue-loader-v15 took 2.93 secs
  module count = 2340
vue-style-loader, and 
css-loader, and 
postcss-loader took 0.373 secs
  module count = 21
vue-style-loader, and 
css-loader, and 
postcss-loader, and 
sass-loader took 0.197 secs
  module count = 4
html-webpack-plugin took 0.141 secs
  module count = 1
@vue/vue-loader-v15, and 
vue-style-loader, and 
css-loader, and 
postcss-loader, and 
@vue/vue-loader-v15 took 0.016 secs
  module count = 6

但CSS相关加载器耗时还是非常长,没搜到怎么优化。。。。


webpack5新增了一个懒加载属性 lazyCompilation, 还可以使用 DllPlugin 进行分包,都能提升运行速度; 后面看看

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2394432.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

国产化Excel处理组件Spire.XLS教程:如何使用 C# 将 Excel(XLS 或 XLSX)文件转换为 PDF

Excel 是常见的数据处理与呈现工具,但直接共享 Excel 文件可能面临格式错乱、兼容性不足或数据泄露的风险。为了保证文档在不同平台和终端上的稳定展示,开发者常常需要将 Excel 文件转换为 PDF 格式。 本文将详细介绍如何使用 C#和.NET Excel 库——Spi…

B3623 枚举排列(递归实现排列型枚举)

B3623 枚举排列(递归实现排列型枚举) - 洛谷 题目描述 今有 n 名学生,要从中选出 k 人排成一列拍照。 请按字典序输出所有可能的排列方式。 输入格式 仅一行,两个正整数 n,k。 输出格式 若干行,每行 k 个正整数…

Fine Pruned Tiled Light Lists(精细删减的分块光照列表)

概括 在这篇文章, 我将介绍一种Tiled Light 变体,主要针对AMD Graphics Core Next(GCN)架构进行优化,我们的方法应用于游戏 古墓丽影:崛起 中,特别是我们在通过光列表生成和阴影贴图渲染之间交错进行异步计…

openresty+lua+redis把非正常访问的域名加入黑名单

一、验证lua geoIp2是否缺少依赖 1、执行命令 /usr/local/openresty/bin/opm get anjia0532/lua-resty-maxminddb 执行安装命令报错,缺少Digest/MD5依赖 2、Digest/MD5依赖 yum -y install perl-Digest-MD5 GeoIP2 lua库依赖动态库安装,lua库依赖libmaxminddb实…

使用Mathematica绘制随机多项式的根

使用ListPlot和NSolve直接绘制: (*返回系数为r和s之间整数的n次随机多项式*) eq[n_, r_, s_] : RandomInteger[{r, s}, {n}] . Array[Power[x, # - 1] &, n] (*返回给定随机多项式的根所对应的笛卡尔坐标*) sol[n_, r_, s_] : {Re[#], Im[#]} & / (x /.…

IEEE PRMVAI 2025 WS 26:计算机视觉前沿 Workshop 来袭!

宝子们,搞计算机视觉和深度学习的看过来啦!🎉 2025 年 IEEE 第三届模式识别、机器视觉和人工智能国际会议里,Workshop 26 简直是科研宝藏地! 这次 Workshop 聚焦 “Deep learning - based low - level models for comp…

360浏览器设置主题

设置默认主题: 1.右上角有个皮肤按钮 进来后,右边有个回复默认皮肤按钮。 换成彩色皮肤后,找按钮不太好找了。

最卸载器——Geek Uninstaller 使用指南

Geek Uninstaller 是一款轻量级的第三方卸载工具,专为 Windows 系统设计,提供比系统默认卸载器更彻底的应用清除能力。它体积小、绿色免安装,使用起来简单直观,但功能却不含糊。 一、为什么要用 Geek Uninstaller? Wi…

应急响应靶机-web3-知攻善防实验室

题目: 1.攻击者的两个IP地址 2.攻击者隐藏用户名称 3.三个攻击者留下的flag 密码:yj123456 解题: 1.攻击者的两个IP地址 一个可能是远程,D盾,404.php,192.168.75.129 找到远程连接相关的英文,1149代表远程连接成功…

【基于SpringBoot的图书购买系统】Redis中的数据以分页的形式展示:从配置到前后端交互的完整实现

引言 在当今互联网应用开发中,高性能和高并发已经成为系统设计的核心考量因素。Redis作为一款高性能的内存数据库,以其快速的读写速度、丰富的数据结构和灵活的扩展性,成为解决系统缓存、高并发访问等场景的首选技术之一。在图书管理系统中&…

Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程

Jupyter MCP 服务器是基于模型上下文协议(Model Context Protocol, MCP)的 Jupyter 环境扩展组件,它能够实现大型语言模型与实时编码会话的无缝集成。该服务器通过标准化的协议接口,使 AI 模型能够安全地访问和操作 Jupyter 的核心…

PMO价值重构:从项目管理“交付机器”到“战略推手”

在数字化转型浪潮中,项目管理办公室(PMO)正经历着前所未有的角色蜕变。传统上,PMO往往被视为项目管理的“交付机器”,专注于项目的按时交付和资源分配。然而,随着企业对战略执行的重视,PMO正逐渐…

零知开源——STM32F407VET6驱动Flappy Bird游戏教程

简介 本教程使用STM32F407VET6零知增强板驱动3.5寸TFT触摸屏实现经典Flappy Bird游戏。通过触摸屏控制小鸟跳跃,躲避障碍物柱体,挑战最高分。项目涉及STM32底层驱动、图形库移植、触摸控制和游戏逻辑设计。 目录 简介 一、硬件准备 二、软件架构 三、…

力扣HOT100之动态规划:300. 最长递增子序列

这道题之前刷代码随想录的时候也刷过,现在又给忘完了。自己尝试着写了一下,发现怎么写都写不对,直接去看视频了。。我自己写的时候的定义是:考虑下标0 ~ i范围内索赔能取到的最长严格递增子序列的长度,后面发现在写递推…

在win10/11下Node.js安装配置教程

下载安装 官网链接https://nodejs.org/zh-cn 下载好以后双击打开,点击下一步 勾选,然后下一步 选择路径、下一步 下一步 配置环境 找到我们安装的文件夹,创建两个文件夹 node_global node_cache 在CMD中配置路径 npm config set p…

飞致云开源社区月度动态报告(2025年5月)

自2023年6月起,中国领先的开源软件公司飞致云以月度为单位发布《飞致云开源社区月度动态报告》,旨在向广大社区用户同步飞致云旗下系列开源软件的发展情况,以及当月主要的产品新版本发布、社区运营成果等相关信息。 飞致云开源运营数据概览&…

压缩包方式在Linux和Windows下安装mongodb

目录 安装流程安装实例1. Linux安装2. Windows安装 总结 安装流程 zip方式安装 优点:自定义性较高,可以自己控制数据、日志等文件的位置 1、下载安装包 2、解压安装包 3、创建各类文件路径 4、配置conf文件 5、使用自定义配置文件启动 安装实例 1. Li…

智慧场馆:科技赋能的艺术盛宴

智慧场馆作为城市公共服务设施数字化转型的典型代表,通过深度融合新一代信息技术,构建起全方位、智能化的运营管理体系。其功能架构不仅提升了场馆本身的运营效能,更重塑了公共服务体验模式,展现出显著的社会价值和商业潜力。 一…

《ChatGPT o3抗命:AI失控警钟还是成长阵痛?》

ChatGPT o3 “抗命” 事件起底 在人工智能的飞速发展进程中,OpenAI 于 2025 年推出的 ChatGPT o3 推理模型,犹如一颗重磅炸弹投入了技术的海洋,激起千层浪。它被视为 “推理模型” 系列的巅峰之作,承载着赋予 ChatGPT 更强大问题解…

【sa-token】 sa-token非 web 上下文无法获取 HttpServletRequest。

Springboot cloud gateway集成sa-token中报错 cn.dev33.satoken.exception.NotWebContextException: 非 web 上下文无法获取 HttpServletRequestat cn.dev33.satoken.spring.SpringMVCUtil.getRequest(SpringMVCUtil.java:45) ~[sa-token-spring-boot-starter-1.38.0.jar:?]官…