目录标题
一、Vite 核心功能解析
🔧 零配置启动
# 创建新项目
npm create vite@latest my-vue-app -- --template vue-ts
# 开发模式启动
npm run dev
📂 项目结构设计
my-vue-app/
├── src/
│ ├── assets/ # 静态资源
│ ├── components/ # 公共组件
│ ├── composables/ # 组合式函数
│ ├── router/ # 路由配置
│ └── main.ts # 入口文件
├── index.html # 主页面
└── vite.config.ts # 构建配置
二、进阶配置详解
🚀 多环境构建方案
// vite.config.ts
import { defineConfig, loadEnv } from 'vite';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), 'VITE_');
return {
define: {
__APP_ENV__: JSON.stringify(env.VITE_API_BASE)
},
build: {
outDir: `dist/${mode}`,
rollupOptions: {
output: {
entryFileNames: `[name]-${mode}.[hash].js`,
chunkFileNames: `[name]-${mode}.[hash].js`
}
}
}
};
});
📦 依赖优化策略
// 手动指定预构建依赖
optimizeDeps: {
include: [
'vue',
'vue-router',
'@vueuse/core',
'lodash-es'
],
exclude: ['vue-demi']
}
三、性能优化实战
🔥 分包策略配置
build: {
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
if (id.includes('lodash')) return 'vendor-lodash';
if (id.includes('echarts')) return 'vendor-echarts';
return 'vendor';
}
}
}
}
}
📸 图片资源优化
// 使用 vite-plugin-image-optimizer
import { imageOptimizer } from 'vite-plugin-image-optimizer';
plugins: [
imageOptimizer({
png: { quality: 80 },
jpeg: { quality: 85 },
webp: { lossless: false }
})
]
四、企业级解决方案
🔌 微前端集成方案
// 主应用配置
import { defineConfig } from 'vite';
import federation from '@originjs/vite-plugin-federation';
export default defineConfig({
plugins: [
federation({
name: 'host-app',
remotes: {
remoteApp: 'http://localhost:5001/assets/remoteEntry.js'
},
shared: ['vue', 'pinia']
})
]
});
🔒 安全加固配置
server: {
headers: {
'Content-Security-Policy': `default-src 'self';`,
'X-Content-Type-Options': 'nosniff'
},
hmr: {
protocol: 'wss', // 生产环境禁用HMR
port: 443
}
}
五、调试与监控
🐞 调试配置
// 开启sourcemap
build: {
sourcemap: 'hidden', // 生产环境隐藏sourcemap
minify: 'terser',
terserOptions: {
sourceMap: true
}
}
// Chrome性能分析插件
plugins: [
{
name: 'devtools',
apply: 'serve',
transform(code, id) {
if (id.includes('main.ts')) {
return code.replace('createApp(App)', 'createApp(App).use(devtools)');
}
}
}
]
📈 构建监控面板
# 安装分析插件
npm i rollup-plugin-visualizer -D
# 配置插件
import { visualizer } from 'rollup-plugin-visualizer';
plugins: [
visualizer({
filename: './stats/stats.html',
open: true,
gzipSize: true
})
]
六、实战案例:电商平台优化
📊 性能对比数据
| 优化项 | 优化前 | 优化后 | 提升幅度 |
|---|---|---|---|
| 冷启动速度 | 2.8s | 0.9s | 68% |
| 热更新速度 | 850ms | 120ms | 86% |
| 构建产物体积 | 4.2MB | 1.8MB | 57% |
🔗 核心配置代码
// 动态按需加载
const routes = [
{
path: '/product/:id',
component: () => import('../views/ProductDetail.vue'),
meta: {
preloadPriority: 1 // 高优先级预加载
}
}
];
// 配置异步加载策略
build: {
dynamicImportVarsOptions: {
exclude: [/components\/.*/] // 排除公共组件
}
}
七、扩展资源推荐
📚 官方生态工具
- vite-plugin-pwa:PWA 支持
- vite-plugin-checker:类型检查
- vitest:单元测试框架
💡 调试技巧
# 查看构建详情
npx vite build --debug
# 生成依赖图谱
npx vite inspect
到这里,这篇文章就和大家说再见啦!我的主页里还藏着很多 篇 前端 实战干货,感兴趣的话可以点击头像看看,说不定能找到你需要的解决方案~
创作这篇内容花了很多的功夫。如果它帮你解决了问题,或者带来了启发,欢迎:
点个赞❤️ 让更多人看到优质内容
关注「前端极客探险家」🚀 每周解锁新技巧
收藏文章⭐️ 方便随时查阅
📢 特别提醒:
转载请注明原文链接,商业合作请私信联系
感谢你的阅读!我们下篇文章再见~ 💕


424

被折叠的 条评论
为什么被折叠?



