Vite 构建优化:别再让你的构建慢得像蜗牛

张开发
2026/4/21 2:52:12 15 分钟阅读

分享文章

Vite 构建优化:别再让你的构建慢得像蜗牛
Vite 构建优化别再让你的构建慢得像蜗牛一、引言又到了我这个毒舌工匠上线的时间了今天咱们来聊聊 Vite 构建优化这个话题。Vite 作为现代前端构建工具以其快速的开发服务器著称但很多人在生产构建时却发现它慢得像蜗牛这是怎么回事别着急我来给你治治这个病。二、Vite 构建慢的常见原因1. 依赖分析耗时问题Vite 在构建时需要分析所有依赖这个过程可能会非常耗时尤其是当项目依赖很多时。原因Vite 默认会分析所有依赖包括那些在生产环境中可能不需要的依赖。2. 代码分割不合理问题代码分割配置不合理导致生成的 chunk 过多或过大。原因默认的代码分割策略可能不适合你的项目需要根据实际情况进行调整。3. 资源处理耗时问题图片、字体等资源处理耗时过长。原因Vite 默认的资源处理策略可能不够高效需要进行优化。4. TypeScript 编译耗时问题TypeScript 编译耗时过长尤其是当项目较大时。原因TypeScript 类型检查和编译需要时间尤其是当类型定义复杂时。三、Vite 构建优化技巧1. 优化依赖分析使用 optimizeDeps配置optimizeDeps.include明确指定需要预构建的依赖配置optimizeDeps.exclude排除不需要预构建的依赖示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; export default defineConfig({ plugins: [react()], optimizeDeps: { include: [react, react-dom, axios, lodash], exclude: [some-heavy-dependency], }, });2. 优化代码分割使用 manualChunks手动配置代码分割策略将常用的依赖打包成独立的 chunk避免生成过多的小 chunk示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; export default defineConfig({ plugins: [react()], build: { rollupOptions: { output: { manualChunks: { vendor: [react, react-dom], utils: [axios, lodash], router: [react-router-dom], state: [zustand], }, }, }, }, });3. 优化资源处理使用 assetsInclude明确指定需要处理的资源类型避免不必要的资源处理示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; export default defineConfig({ plugins: [react()], assetsInclude: [**/*.svg, **/*.png, **/*.jpg, **/*.jpeg, **/*.gif], });使用 vite-plugin-imagemin压缩图片资源减少资源大小示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; import imagemin from vite-plugin-imagemin; export default defineConfig({ plugins: [ react(), imagemin({ gifsicle: { optimizationLevel: 7, interlaced: false, }, optipng: { optimizationLevel: 7, }, mozjpeg: { quality: 80, }, pngquant: { quality: [0.8, 0.9], speed: 4, }, svgo: { plugins: [ { name: removeViewBox, active: false, }, { name: removeEmptyAttrs, active: false, }, ], }, }), ], });4. 优化 TypeScript 编译使用 tsconfig.json 优化配置tsconfig.json减少类型检查的范围使用isolatedModules提高编译速度示例// tsconfig.json { compilerOptions: { target: ES2020, useDefineForClassFields: true, lib: [ES2020, DOM, DOM.Iterable], module: ESNext, skipLibCheck: true, moduleResolution: bundler, allowImportingTsExtensions: true, resolveJsonModule: true, isolatedModules: true, noEmit: true, jsx: react-jsx, strict: true, noUnusedLocals: true, noUnusedParameters: true, noFallthroughCasesInSwitch: true }, include: [src], references: [{ path: ./tsconfig.node.json }] }使用 esbuild 进行 TypeScript 编译Vite 默认使用 esbuild 进行 TypeScript 编译速度比 tsc 快很多确保开启 esbuild 的 TypeScript 编译功能5. 其他优化技巧使用 cacheDir配置cacheDir缓存构建结果减少重复构建的时间示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; export default defineConfig({ plugins: [react()], cacheDir: ./node_modules/.vite, });使用 terser 压缩配置build.minify为terser获得更好的压缩效果示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; export default defineConfig({ plugins: [react()], build: { minify: terser, terserOptions: { compress: { drop_console: true, drop_debugger: true, }, }, }, });使用 sourcemap合理配置 sourcemap在开发环境使用sourcemap: true在生产环境使用sourcemap: hidden示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; export default defineConfig({ plugins: [react()], build: { sourcemap: process.env.NODE_ENV production ? hidden : true, }, });四、完整的 Vite 配置示例// vite.config.js import { defineConfig } from vite; import react from vitejs/plugin-react; import imagemin from vite-plugin-imagemin; import path from path; export default defineConfig({ plugins: [ react(), imagemin({ gifsicle: { optimizationLevel: 7, interlaced: false, }, optipng: { optimizationLevel: 7, }, mozjpeg: { quality: 80, }, pngquant: { quality: [0.8, 0.9], speed: 4, }, svgo: { plugins: [ { name: removeViewBox, active: false, }, { name: removeEmptyAttrs, active: false, }, ], }, }), ], resolve: { alias: { : path.resolve(__dirname, src), }, }, optimizeDeps: { include: [react, react-dom, axios, lodash, react-router-dom, zustand], }, build: { target: es2015, minify: terser, terserOptions: { compress: { drop_console: true, drop_debugger: true, }, }, sourcemap: process.env.NODE_ENV production ? hidden : true, rollupOptions: { output: { manualChunks: { vendor: [react, react-dom], utils: [axios, lodash], router: [react-router-dom], state: [zustand], }, }, }, }, cacheDir: ./node_modules/.vite, });五、总结Vite 构建优化是一个持续的过程需要根据项目的实际情况进行调整。别再让你的构建慢得像蜗牛了使用这些优化技巧让你的构建过程更加快速、高效。最后我想说构建优化不是一次性的工作而是一个持续的过程。你需要不断地分析构建结果找出优化点持续改进。

更多文章