Files
gloabl_award_front/vite.config.ts

191 lines
5.1 KiB
TypeScript
Raw Normal View History

2026-02-06 15:10:17 +08:00
import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import DefineOptions from 'unplugin-vue-define-options/vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
2026-03-24 13:25:35 +08:00
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
2026-02-06 15:10:17 +08:00
// console.log(process)
// console.log(import.meta.env.VITE_APP_URL)
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
2026-02-06 17:20:57 +08:00
const env = loadEnv(mode, process.cwd())
2026-03-24 13:25:35 +08:00
const isProduction = mode === 'production'
2026-02-06 15:10:17 +08:00
2026-02-06 17:20:57 +08:00
return {
plugins: [
vue(),
DefineOptions(),
AutoImport({
2026-03-24 13:25:35 +08:00
resolvers: [],
2026-02-06 17:20:57 +08:00
imports: [
'vue',
'vue-router',
{
'lodash-es': [
'debounce',
'cloneDeep',
'cloneDeepWith',
'isBoolean',
'isString',
'isNumber',
'isArray',
'isDate',
'isFunction',
'isNaN',
'isNull',
'isObject',
'isUndefined'
]
}
2026-03-24 13:25:35 +08:00
]
2026-02-06 17:20:57 +08:00
}),
Components({
2026-03-24 13:25:35 +08:00
resolvers: [AntDesignVueResolver({ importStyle: false })]
2026-02-06 17:20:57 +08:00
}),
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
symbolId: 'icon-[dir]-[name]',
2026-03-24 13:25:35 +08:00
inject: 'body-last'
2026-02-06 17:20:57 +08:00
})
],
define: {
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false
},
css: {
preprocessorOptions: {
less: {
modifyVars: {
'primary-color': '#ec6800'
},
javascriptEnabled: true,
additionalData: `@import "${path.resolve(__dirname, 'src/assets/css/style.less')}";`
}
}
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
_c: fileURLToPath(new URL('./src/components', import.meta.url))
}
},
server: {
2026-03-24 13:25:35 +08:00
host: '0.0.0.0',
port: 8088,
open: true,
strictPort: true,
2026-02-06 17:20:57 +08:00
hmr: {
overlay: true
},
proxy: {
'/api': {
target: env.VITE_APP_URL,
2026-03-24 13:25:35 +08:00
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '/api')
}
}
},
// ========== 打包配置 ==========
build: {
// 构建目标浏览器
target: 'es2015',
// 是否生成 sourcemap
sourcemap: isProduction ? false : 'eval',
// 打包文件输出目录
outDir: 'dist',
// 静态资源输出目录(相对于 outDir
assetsDir: 'assets',
// 小于此阈值的导入将内联为 base64
assetsInlineLimit: 4096,
// 启用 CSS 代码分割
cssCodeSplit: true,
// 公共基础路径
publicDir: 'public',
// 启用 gzip 压缩大小限制
chunkSizeWarningLimit: 500,
// 自定义 chunk 分包策略
rollupOptions: {
output: {
// 静态资源打包命名
assetFileNames: (assetInfo) => {
const info = assetInfo.name || ''
if (/\.(woff|woff2?|eot|ttf|otf)$/i.test(info)) {
return 'assets/fonts/[name]-[hash][extname]'
}
if (/\.(png|jpe?g|gif|svg|webp|avif|ico)$/i.test(info)) {
return 'assets/images/[name]-[hash][extname]'
}
if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)$/i.test(info)) {
return 'assets/media/[name]-[hash][extname]'
}
return 'assets/[name]-[hash][extname]'
},
// chunk 分包命名
chunkFileNames: 'assets/js/[name]-[hash].js',
// 入口文件命名
entryFileNames: 'assets/js/[name]-[hash].js',
// 手动分包策略
manualChunks: {
// 核心框架
'vue-core': ['vue', 'vue-router', 'pinia', 'pinia-plugin-persistedstate'],
// 国际化
'vue-i18n-core': ['vue-i18n'],
// UI 框架
'ant-design': ['ant-design-vue'],
// 工具库
'utils': ['lodash-es', 'axios'],
// 其他依赖
'vendor': ['gsap', 'crypto-js']
}
}
},
// 依赖预构建优化
optimizeDeps: {
include: [
'vue',
'vue-router',
'pinia',
'vue-i18n',
'ant-design-vue',
'axios',
'lodash-es',
'gsap',
'crypto-js'
]
},
// 压缩配置
minify: 'terser',
terserOptions: {
compress: {
drop_console: isProduction, // 生产环境移除 console
drop_debugger: isProduction, // 生产环境移除 debugger
pure_funcs: isProduction ? ['console.log'] : []
},
format: {
comments: false // 移除注释
}
}
},
// 预览服务器配置
preview: {
host: '0.0.0.0',
port: 8088,
open: true,
proxy: {
'/api': {
target: env.VITE_APP_URL,
changeOrigin: true
2026-02-06 17:20:57 +08:00
}
}
}
}
2026-02-06 15:10:17 +08:00
})