97 lines
2.8 KiB
TypeScript
97 lines
2.8 KiB
TypeScript
import { fileURLToPath, URL } from 'node:url'
|
||
import { loadEnv, defineConfig, ConfigEnv, ViteConfig } from 'vite'
|
||
import vue from '@vitejs/plugin-vue'
|
||
import vueDevTools from 'vite-plugin-vue-devtools'
|
||
|
||
// 自动导入插件
|
||
import AutoImport from 'unplugin-auto-import/vite'
|
||
import Components from 'unplugin-vue-components/vite'
|
||
|
||
// Element Plus 组件自动导入
|
||
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
||
|
||
// https://vite.dev/config/
|
||
export default defineConfig(({ mode }: ConfigEnv): ViteConfig => {
|
||
const isProduction = mode === 'production'
|
||
const env = loadEnv(mode, process.cwd())
|
||
return {
|
||
plugins: [
|
||
vue(),
|
||
// 生产环境移除 devTools 插件(减少产物体积)
|
||
...(!isProduction ? [vueDevTools()] : []),
|
||
AutoImport({
|
||
resolvers: [ElementPlusResolver()],
|
||
imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
|
||
dts: 'src/auto-imports.d.ts',
|
||
eslintrc: { enabled: true },
|
||
}),
|
||
Components({
|
||
resolvers: [ElementPlusResolver()],
|
||
dts: 'src/components.d.ts',
|
||
dirs: ['src/components'],
|
||
}),
|
||
],
|
||
resolve: {
|
||
alias: {
|
||
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
||
},
|
||
},
|
||
|
||
// ====== 增强版生产构建配置 ======
|
||
build: {
|
||
outDir: 'dist',
|
||
assetsDir: 'assets',
|
||
|
||
// 生产环境 sourcemap 配置(可选:隐藏源码但保留行映射)
|
||
sourcemap: isProduction ? 'hidden' : true,
|
||
|
||
// chunk 分割策略(优化加载性能)
|
||
rollupOptions: {
|
||
output: {
|
||
manualChunks: {
|
||
// 将第三方库单独分包
|
||
'element-plus': ['element-plus', '@element-plus/icons-vue'],
|
||
vendor: ['vue', 'vue-router', 'pinia'],
|
||
utils: ['axios', 'dayjs', 'crypto-js', '@vueuse/core'],
|
||
},
|
||
// 文件名带 hash(缓存优化)
|
||
chunkFileNames: 'assets/js/[name]-[hash].js',
|
||
entryFileNames: 'assets/js/[name]-[hash].js',
|
||
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
|
||
},
|
||
},
|
||
|
||
// 压缩配置
|
||
minify: 'esbuild',
|
||
esbuild: {
|
||
drop: isProduction ? ['console', 'debugger'] : [],
|
||
minifyIdentifiers: true,
|
||
minifySyntax: true,
|
||
minifyWhitespace: true,
|
||
},
|
||
|
||
// 大文件警告阈值(默认500KB,可调整)
|
||
chunkSizeWarningLimit: 1000,
|
||
},
|
||
|
||
// 生产环境不显示错误遮罩
|
||
define: {
|
||
__VUE_PROD_DEVTOOLS__: 'false',
|
||
},
|
||
|
||
server: {
|
||
host: true,
|
||
port: 9999,
|
||
https: false,
|
||
open: true,
|
||
proxy: {
|
||
'/api': {
|
||
target: env.VITE_API_BASE_URL,
|
||
changeOrigin: true,
|
||
rewrite: (path) => path.replace(/^\/api/, ''),
|
||
},
|
||
},
|
||
},
|
||
}
|
||
})
|