vue+vite 减缓首屏加载压力和性能优化

vue+vite 减缓首屏加载压力和性能优化

在vue+vite构建的工程里面,性能优化分为开发环境和打包后的生产环境,作为开发首先需要把找个了解清楚,接下来分别解析在开发和生产处理的方案,不多说,直接上代码。

1、预加载项目必需的组件

c 复制代码
 // 预加载项目必需的组件
    optimizeDeps: {
      include: [
        "vue",
        "vue-router",
        "pinia",
        "axios",
        "@vueuse/core",
        "sortablejs",
        "exceljs",
        "path-to-regexp",
        "echarts",
        "@wangeditor/editor",
        "@wangeditor/editor-for-vue",
        "vue-i18n",
        "vue-echarts",
        "echarts-liquidfill",
        "path-browserify",
        "lodash",
        "moment",
      ],
    },

这个配置是写入vite.config.ts 配置文件里面,optimizeDeps和plugins 同级别

2、使用 vite-plugin-optimize-persist 自动生成最优预构建配置

c 复制代码
npm install vite-plugin-optimize-persist -D
c 复制代码
import OptimizationPersist from 'vite-plugin-optimize-persist'

export default {
  plugins: [
    OptimizationPersist()
  ]
}

2、模块加载优化

按需加载架构

c 复制代码
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default {
  plugins: [
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ]
}

动态导入辅助

c 复制代码
<script setup>
import { defineAsyncComponent } from 'vue'
const HeavyComponent = defineAsyncComponent(() => 
  import('./components/HeavyComponent.vue')
)
</script>

3、HMR 热更新优化

文件监听策略

c 复制代码
export default {
  server: {
    watch: {
      usePolling: true,
      interval: 1000,
      ignored: [
        '**/node_modules/**',
        '**/.git/**',
        '**/dist/**'
      ]
    }
  }
}

智能缓存策略

c 复制代码
export default {
  cacheDir: './.custom_vite_cache',
  build: {
    rollupOptions: {
      cache: false // 开发环境保持启用
    }
  }
}

4 、环境配置优化

c 复制代码
NODE_OPTIONS="--max-old-space-size=4096" vite

5、代码模式优化

Tree-shaking 增强

c 复制代码
export default {
  build: {
    rollupOptions: {
      treeshake: {
        preset: 'recommended',
        moduleSideEffects: (id) => !/\.css$/.test(id)
      }
    }
  }
}

Dead Code 检测

c 复制代码
import { defineConfig } from 'vite'
import deadcode from 'vite-plugin-deadcode'

export default defineConfig({
  plugins: [
    deadcode({
      patterns: ['src/**/*.(js|vue)']
    })
  ]
})

6、高级优化技巧

GPU 加速渲染

c 复制代码
.gpu-accelerate {
  transform: translateZ(0);
  backface-visibility: hidden;
  perspective: 1000px;
}

内存优化策略

c 复制代码
// 使用 WeakMap 存储大型临时数据
const cache = new WeakMap()

export function useHeavyCalculation() {
  // 内存敏感操作
}

7、架构级优化

微前端优化

c 复制代码
// 子应用配置
export default {
  build: {
    lib: {
      entry: './src/main.js',
      name: 'vue3Module',
      formats: ['es']
    }
  }
}

Web Workers 优化

c 复制代码
// worker-loader.js
export default function (config) {
  return {
    name: 'worker-loader',
    transform(code, id) {
      if (id.includes('?worker')) {
        return `
          export default function WorkerWrapper() {
            return new Worker(
              URL.createObjectURL(
                new Blob([${JSON.stringify(code)}], 
                { type: 'application/javascript' }
              )
            )
          }
        `
      }
    }
  }
}

打包压缩配置

c 复制代码
 plugins: [
    viteCompression({
      // gzip静态资源压缩配置
      verbose: true, // 是否在控制台输出压缩结果
      disable: false, // 是否禁用压缩
      threshold: 10240, // 启用压缩的文件大小限制
      algorithm: 'gzip', // 采用的压缩算法
      ext: '.gz' // 生成的压缩包后缀
    })
  ],
相关推荐
kyriewen1 小时前
产品经理把PRD写成“天书”,我用AI半小时重写了一遍,他当场愣住
前端·ai编程·cursor
humcomm1 小时前
元框架的工作原理详解
前端·前端框架
canonical_entropy2 小时前
Attractor Before Harness: AI 大规模开发的方法论
前端·aigc·ai编程
zhangxingchao2 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒2 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
Honor丶Onlyou2 小时前
VS Code 右键菜单修复记录
前端
PILIPALAPENG2 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
JYeontu2 小时前
轮播图不够惊艳?试下这个立体卡片轮播图
前端·javascript·css
张就是我1065922 小时前
从前端角度理解 CVE-2026-31431
前端
AGoodrMe2 小时前
swift基础之async/await
前端·ios