news 2026/4/16 15:35:42

Vue——vue3 打包优化与资源压缩

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue——vue3 打包优化与资源压缩

背景问题:
需要优化打包体积和加载速度。

方案思考:
通过代码分割、压缩和资源优化来减少打包体积。

具体实现:
Vite配置优化:

// vite.config.jsimport{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'import{compression}from'vite-plugin-compression'import{visualizer}from'rollup-plugin-visualizer'exportdefaultdefineConfig({plugins:[vue(),// Gzip压缩compression({algorithm:'gzip',threshold:10240// 超过10KB的文件进行压缩}),// 包分析工具visualizer({filename:'./dist/stats.html',open:true,gzipSize:true,brotliSize:true})],build:{rollupOptions:{output:{// 分包优化manualChunks:{// Vue核心库vue:['vue','vue-router','pinia'],// UI组件库element:['element-plus'],// 工具库utils:['lodash-es','axios'],// 图表库charts:['echarts']},// 或者使用函数方式更灵活地分包manualChunks(id){if(id.includes('node_modules')){if(id.includes('vue')||id.includes('pinia')||id.includes('vue-router')){return'vue'}if(id.includes('element-plus')){return'element'}if(id.includes('lodash')||id.includes('axios')){return'utils'}if(id.includes('echarts')){return'charts'}return'vendor'}}}},// 压缩配置minify:'terser',terserOptions:{compress:{drop_console:true,// 移除consoledrop_debugger:true,// 移除debuggerpure_funcs:['console.log']// 移除指定函数},format:{comments:false// 移除注释}},// 启用CSS分割cssCodeSplit:true},// 预加载脚本optimizeDeps:{include:['element-plus/lib/locale/lang/zh-cn'],// 预加载国际化文件}})

代码分割优化:

// utils/lazy-load.js// 动态导入工具函数exportclassLazyLoad{// 带错误处理的懒加载staticasynclazyImport(importFunction,retries=3){for(leti=0;i<retries;i++){try{returnawaitimportFunction()}catch(error){if(i===retries-1){throwerror}// 延迟后重试awaitnewPromise(resolve=>setTimeout(resolve,1000))}}}// 路由懒加载staticrouteLazyLoader(path){return()=>import(`../views${path}.vue`)}// 组件懒加载staticcomponentLazyLoader(path){return()=>import(`../components${path}.vue`)}// 带加载提示的懒加载staticasynclazyWithLoading(importFunction,setLoading){try{setLoading&&setLoading(true)constmodule=awaitimportFunction()returnmodule}finally{setLoading&&setLoading(false)}}}

路由懒加载:

// router/index.jsimport{createRouter,createWebHistory}from'vue-router'constroutes=[{path:'/',name:'Home',component:()=>import('@/views/Home.vue'),// 懒加载meta:{title:'首页'}},{path:'/about',name:'About',component:()=>import('@/views/About.vue'),// 懒加载meta:{title:'关于我们'}},// 按功能分组的懒加载{path:'/user',component:()=>import('@/layouts/index.vue'),children:[{path:'profile',name:'UserProfile',component:()=>import('@/views/user/Profile.vue'),meta:{title:'用户资料'}},{path:'settings',name:'UserSettings',component:()=>import('@/views/user/Settings.vue'),meta:{title:'用户设置'}}]},// 大型功能模块单独打包{path:'/admin',name:'Admin',component:()=>import('@/views/admin/index.vue'),meta:{title:'管理后台'},children:[{path:'dashboard',name:'AdminDashboard',component:()=>import('@/views/admin/Dashboard.vue'),meta:{title:'管理面板'}}]}]constrouter=createRouter({history:createWebHistory(),routes})exportdefaultrouter
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 7:22:44

英雄联盟个性化美化神器:LeaguePrank完全使用指南

英雄联盟个性化美化神器&#xff1a;LeaguePrank完全使用指南 【免费下载链接】LeaguePrank 项目地址: https://gitcode.com/gh_mirrors/le/LeaguePrank 想要在英雄联盟中展现与众不同的游戏形象吗&#xff1f;LeaguePrank是一款功能强大的LOL美化工具&#xff0c;通过…

作者头像 李华
网站建设 2026/4/16 7:22:47

MediaPipe Holistic参数调优:关键点过滤阈值设置

MediaPipe Holistic参数调优&#xff1a;关键点过滤阈值设置 1. 引言&#xff1a;AI 全身全息感知的技术挑战 随着虚拟现实、数字人和智能交互系统的快速发展&#xff0c;对全维度人体动作捕捉的需求日益增长。传统的单模态检测&#xff08;如仅姿态或仅手势&#xff09;已无…

作者头像 李华
网站建设 2026/4/16 1:06:13

5分钟搞定纪念币预约的终极秘籍

5分钟搞定纪念币预约的终极秘籍 【免费下载链接】auto_commemorative_coin_booking 项目地址: https://gitcode.com/gh_mirrors/au/auto_commemorative_coin_booking 还在为抢不到心仪的纪念币而emo吗&#xff1f;每次打开预约页面&#xff0c;看着秒光的库存&#xff…

作者头像 李华
网站建设 2026/4/16 7:29:00

Holistic Tracking部署监控:性能指标可视化教程

Holistic Tracking部署监控&#xff1a;性能指标可视化教程 1. 引言 1.1 业务场景描述 在虚拟现实、数字人驱动、远程交互和智能监控等前沿应用中&#xff0c;对人类行为的全面理解已成为核心技术需求。传统的单模态感知&#xff08;如仅姿态或仅手势&#xff09;已无法满足…

作者头像 李华
网站建设 2026/4/15 17:20:26

Holistic Tracking降本部署案例:CPU极速版节省90%算力成本

Holistic Tracking降本部署案例&#xff1a;CPU极速版节省90%算力成本 1. 技术背景与业务挑战 在虚拟人、元宇宙、AI直播等前沿应用场景中&#xff0c;对全维度人体动作捕捉的需求日益增长。传统方案通常依赖多模型并行推理——分别运行人脸、手势和姿态模型&#xff0c;不仅…

作者头像 李华
网站建设 2026/4/16 2:54:15

screen+实现多语言HMI界面:项目应用

用 screen 构建多语言HMI&#xff1a;一次搞定全球部署的实战方案你有没有遇到过这样的场景&#xff1f;一台设备刚在德国交付&#xff0c;客户突然要求下周发往日本——界面语言怎么办&#xff1f;重写代码&#xff1f;重新编译下载&#xff1f;还是干脆再做一套固件&#xff…

作者头像 李华