103 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-05-29 15:03:59 +08:00
import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd())
// 如果环境变量不存在,则使用默认值
const VITE_APP_ENV = env.VITE_APP_ENV || 'production'
const VITE_BASE_ROUTER = env.VITE_BASE_ROUTER || '/'
const VITE_APP_BASE_API = env.VITE_APP_BASE_API || '/prod-api'
return {
// 部署生产环境和开发环境下的URL。
// 默认情况下vite 会假设你的应用是被部署在一个域名的根路径上
// 例如 https://www.boyue.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.boyue.vip/admin/,则设置 baseUrl 为 /admin/。
base: VITE_APP_ENV === 'production' ? VITE_BASE_ROUTER : VITE_BASE_ROUTER,
plugins: createVitePlugins(env, command === 'build'),
resolve: {
// https://cn.vitejs.dev/config/#resolve-alias
alias: {
// 设置路径
'~': path.resolve(__dirname, './'),
// 设置别名
'@': path.resolve(__dirname, './src')
},
// https://cn.vitejs.dev/config/#resolve-extensions
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
},
// vite 相关配置
server: {
port: 8001,
host: true,
open: false, // 启动时是否启动自动打开浏览器
proxy: {
// https://cn.vitejs.dev/config/#server-proxy
'/dev-api': {
target: 'http://222.184.49.22:9799',
changeOrigin: true,
rewrite: (p) => p.replace(/^\/dev-api/, '')
},
'/v3': {
target: 'http://222.184.49.22:9799',
changeOrigin: true,
rewrite: (p) => p.replace(/^\/dev-api/, '')
}
}
},
build: {
// 设置最终构建的浏览器兼容目标
target: 'es2015',
// 构建后是否生成 source map 文件
sourcemap: false,
// 启用/禁用 brotli 压缩大小报告
brotliSize: false,
// chunk 大小警告的限制
chunkSizeWarningLimit: 2000,
// 生产环境移除 console
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
}
},
//fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler'
}
},
postcss: {
plugins: [
{
postcssPlugin: 'internal:charset-removal',
AtRule: {
charset: (atRule) => {
if (atRule.name === 'charset') {
atRule.remove();
}
}
}
}
]
}
},
optimizeDeps: {
include: [
'@/../lib/vform/designer.umd.js',
]
},
// 定义全局常量替换方式
define: {
// 设置API基础路径
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
'process.env': {
VITE_APP_BASE_API: JSON.stringify(VITE_APP_BASE_API)
}
}
}
})