news 2026/5/10 8:42:23

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/5/3 16:41:55

League Akari完整使用指南:让英雄联盟游戏体验翻倍的秘密武器

League Akari完整使用指南&#xff1a;让英雄联盟游戏体验翻倍的秘密武器 【免费下载链接】LeagueAkari ✨兴趣使然的&#xff0c;功能全面的英雄联盟工具集。支持战绩查询、自动秒选等功能。基于 LCU API。 项目地址: https://gitcode.com/gh_mirrors/le/LeagueAkari 还…

作者头像 李华
网站建设 2026/5/10 8:42:07

想做虚拟主播?IndexTTS2帮你搞定情绪化配音

想做虚拟主播&#xff1f;IndexTTS2帮你搞定情绪化配音 在虚拟主播、有声内容和AI数字人快速发展的今天&#xff0c;用户对语音合成的要求早已超越“能说清楚”。他们需要的是富有情感、性格鲜明、具备表现力的声音——那种一听就能打动人心的拟人化表达。然而&#xff0c;大多…

作者头像 李华
网站建设 2026/5/10 8:42:08

OpCore Simplify终极指南:5分钟快速配置黑苹果EFI

OpCore Simplify终极指南&#xff1a;5分钟快速配置黑苹果EFI 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的OpenCore配置而烦恼吗&…

作者头像 李华
网站建设 2026/5/9 9:51:28

全息感知模型部署:MediaPipe Holistic常见问题解决方案

全息感知模型部署&#xff1a;MediaPipe Holistic常见问题解决方案 1. 引言 1.1 AI 全身全息感知的技术背景 随着虚拟现实、数字人和元宇宙应用的兴起&#xff0c;对高精度、低延迟的人体三维感知技术需求日益增长。传统方案往往需要多个独立模型分别处理面部、手势和姿态&a…

作者头像 李华
网站建设 2026/5/7 19:37:01

AI全身感知优化指南:提升Holistic Tracking检测精度的7个技巧

AI全身感知优化指南&#xff1a;提升Holistic Tracking检测精度的7个技巧 1. 引言 1.1 技术背景与挑战 在虚拟现实、数字人驱动、动作捕捉和智能交互等前沿应用中&#xff0c;对人类行为的全维度理解已成为AI视觉系统的核心需求。传统的单模态感知&#xff08;如仅识别人体姿…

作者头像 李华