You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.0 KiB
69 lines
2.0 KiB
import { defineConfig, loadEnv } from 'vite'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import { fileURLToPath, URL } from 'node:url'
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
// 加载环境变量
|
|
const env = loadEnv(mode, process.cwd(), '')
|
|
|
|
// 开发环境必须配置代理目标
|
|
if (mode === 'development') {
|
|
if (!env.VITE_API_PROXY_TARGET) {
|
|
throw new Error(
|
|
'❌ 错误: 开发环境必须配置 VITE_API_PROXY_TARGET 环境变量\n' +
|
|
'请在 .env.development 文件中添加:\n' +
|
|
'VITE_API_PROXY_TARGET=http://czemc.localhost\n' +
|
|
'\n' +
|
|
'示例:\n' +
|
|
'VITE_API_PROXY_TARGET=http://czemc.localhost\n' +
|
|
'或\n' +
|
|
'VITE_API_PROXY_TARGET=http://localhost:8000'
|
|
)
|
|
}
|
|
}
|
|
|
|
return {
|
|
plugins: [vue()],
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url))
|
|
}
|
|
},
|
|
server: {
|
|
port: 3000,
|
|
open: true,
|
|
proxy: mode === 'development' ? {
|
|
// 代理所有 /api 请求到后端服务器
|
|
'/api': {
|
|
target: env.VITE_API_PROXY_TARGET,
|
|
changeOrigin: true,
|
|
secure: false,
|
|
// 如果需要重写路径,可以取消下面的注释
|
|
// rewrite: (path) => path.replace(/^\/api/, '')
|
|
}
|
|
} : undefined
|
|
},
|
|
// 定义全局常量替换
|
|
define: {
|
|
// 确保环境变量在构建时被正确替换
|
|
__APP_ENV__: JSON.stringify(env.VITE_APP_ENV || mode)
|
|
},
|
|
// 构建配置
|
|
build: {
|
|
// 从环境变量读取构建输出目录,如果没有则使用默认值
|
|
outDir: env.VITE_BUILD_OUT_DIR || 'dist',
|
|
sourcemap: mode === 'development',
|
|
// 根据环境输出不同的构建产物名称
|
|
rollupOptions: {
|
|
output: {
|
|
// 可以根据环境自定义输出文件名
|
|
entryFileNames: `assets/[name].${mode}.js`,
|
|
chunkFileNames: `assets/[name].${mode}.js`,
|
|
assetFileNames: `assets/[name].${mode}.[ext]`
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|