news 2026/2/11 10:48:40

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/2/5 6:30:47

Vue——Vue3 + Vite 状态管理篇 之【Pinia 状态管理架构】

背景问题&#xff1a; 需要管理应用的全局状态。 方案思考&#xff1a; 使用 Pinia 作为状态管理工具。 具体实现&#xff1a; 创建 Pinia 实例&#xff1a; // stores/index.js import { createPinia } from piniaconst pinia createPinia()export default pinia// main.js i…

作者头像 李华
网站建设 2026/2/10 13:17:12

OpCore Simplify终极指南:零基础搭建稳定黑苹果系统

OpCore Simplify终极指南&#xff1a;零基础搭建稳定黑苹果系统 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 你是否曾经被复杂的命令行配置吓退&am…

作者头像 李华
网站建设 2026/2/7 21:19:49

Blender MMD Tools终极指南:从零到精通的完整实操手册

Blender MMD Tools终极指南&#xff1a;从零到精通的完整实操手册 【免费下载链接】blender_mmd_tools MMD Tools is a blender addon for importing/exporting Models and Motions of MikuMikuDance. 项目地址: https://gitcode.com/gh_mirrors/bl/blender_mmd_tools 想…

作者头像 李华
网站建设 2026/2/10 4:36:01

MediaPipe Holistic性能测试:不同硬件配置下的表现对比

MediaPipe Holistic性能测试&#xff1a;不同硬件配置下的表现对比 1. 引言 1.1 AI 全身全息感知的技术背景 随着虚拟现实、数字人和智能交互系统的快速发展&#xff0c;对全维度人体动作捕捉的需求日益增长。传统方案往往依赖多模型串联或高成本动捕设备&#xff0c;存在延…

作者头像 李华
网站建设 2026/2/10 23:57:49

纪念币预约终极指南:3步实现自动化抢购的简单方法

纪念币预约终极指南&#xff1a;3步实现自动化抢购的简单方法 【免费下载链接】auto_commemorative_coin_booking 项目地址: https://gitcode.com/gh_mirrors/au/auto_commemorative_coin_booking 还在为每次纪念币预约都手忙脚乱而烦恼吗&#xff1f;纪念币预约自动化…

作者头像 李华
网站建设 2026/2/5 2:34:20

IndexTTS2实测报告:V23情感控制效果远超预期

IndexTTS2实测报告&#xff1a;V23情感控制效果远超预期 在语音合成&#xff08;Text-to-Speech, TTS&#xff09;技术快速演进的今天&#xff0c;用户对“拟人化”表达的需求已从可有可无变为刚性标准。传统的中性语调、机械停顿早已无法满足智能客服、虚拟主播、有声内容创作…

作者头像 李华