init
This commit is contained in:
36
vite.config.js
Normal file
36
vite.config.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { fileURLToPath } from 'url'
|
||||
import { defineConfig, loadEnv } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
|
||||
// Vite 配置
|
||||
export default defineConfig(({ mode }) => {
|
||||
// 加载环境变量
|
||||
const env = loadEnv(mode, process.cwd(), '')
|
||||
|
||||
return {
|
||||
plugins: [vue()],
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': fileURLToPath(new URL('./src', import.meta.url))
|
||||
}
|
||||
},
|
||||
server: {
|
||||
port: 5173,
|
||||
proxy: {
|
||||
'/api': {
|
||||
// 本地开发模式:VITE_SERVER_URL 为空或本地地址时使用代理
|
||||
target: env.VITE_SERVER_URL || `http://localhost:${env.VITE_API_PORT || '3000'}`,
|
||||
changeOrigin: true,
|
||||
// 仅远程部署时改变 origin
|
||||
configure: (proxy) => {
|
||||
if (env.VITE_SERVER_URL && !env.VITE_SERVER_URL.includes('localhost')) {
|
||||
proxy.on('proxyReq', (proxyReq) => {
|
||||
proxyReq.setHeader('origin', env.VITE_SERVER_URL)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user