news 2026/6/15 0:04:01

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/6/15 14:09:28

3步搞定B站视频智能下载:BiliTools完全使用指南

3步搞定B站视频智能下载&#xff1a;BiliTools完全使用指南 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱&#xff0c;支持视频、音乐、番剧、课程下载……持续更新 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

作者头像 李华
网站建设 2026/6/15 13:15:19

AI画质增强优化技巧:Super Resolutio镜像性能提升秘籍

AI画质增强优化技巧&#xff1a;Super Resolution镜像性能提升秘籍 1. 项目背景与技术定位 随着数字图像在社交媒体、安防监控、医疗影像等领域的广泛应用&#xff0c;低分辨率图像带来的信息缺失问题日益突出。传统插值放大方法&#xff08;如双线性、双三次&#xff09;虽然…

作者头像 李华
网站建设 2026/6/10 11:42:01

Holistic Tracking部署总报错?容错机制配置步骤详解

Holistic Tracking部署总报错&#xff1f;容错机制配置步骤详解 1. 引言&#xff1a;AI 全身全息感知 - Holistic Tracking 在虚拟人、数字孪生和元宇宙应用快速发展的今天&#xff0c;全维度人体感知技术正成为连接现实与虚拟世界的核心桥梁。其中&#xff0c;Google 提出的…

作者头像 李华
网站建设 2026/6/15 8:45:16

底层驱动中race condition致crash图解说明

竟然因为一个head就让系统崩了&#xff1f;聊聊驱动里最隐蔽的杀手——竞态条件你有没有遇到过这种问题&#xff1a;代码逻辑明明没问题&#xff0c;测试也跑通了&#xff0c;结果在客户现场偶尔重启一次&#xff0c;抓到的 crash 日志还像是“指针乱飞”&#xff1f;翻遍代码也…

作者头像 李华
网站建设 2026/6/13 19:07:39

本地运行不求人!IndexTTS2完全离线语音合成方案

本地运行不求人&#xff01;IndexTTS2完全离线语音合成方案 1. 引言&#xff1a;为什么需要本地化语音合成&#xff1f; 在当前AI语音技术快速发展的背景下&#xff0c;越来越多的应用场景对数据隐私性、响应实时性和部署灵活性提出了更高要求。传统的云服务语音合成&#xf…

作者头像 李华
网站建设 2026/6/10 13:56:48

戴尔G15散热终极解决方案:tcc-g15完整使用指南

戴尔G15散热终极解决方案&#xff1a;tcc-g15完整使用指南 【免费下载链接】tcc-g15 Thermal Control Center for Dell G15 - open source alternative to AWCC 项目地址: https://gitcode.com/gh_mirrors/tc/tcc-g15 还在为戴尔G15笔记本散热问题而烦恼吗&#xff1f;官…

作者头像 李华