45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
import { fileURLToPath, URL } from 'node:url'
|
|
import { defineConfig } from 'vite'
|
|
import vuePlugin from '@vitejs/plugin-vue'
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
import Components from 'unplugin-vue-components/vite'
|
|
import { VantResolver } from 'unplugin-vue-components/resolvers'
|
|
import * as dotenv from 'dotenv'
|
|
import * as fs from 'fs'
|
|
|
|
export default defineConfig(({ command, mode}) => {
|
|
const NODE_ENV = mode || 'development'
|
|
const envFiles = [
|
|
`.env.${NODE_ENV}`
|
|
]
|
|
for (const file of envFiles) {
|
|
const envConfig = dotenv.parse(fs.readFileSync(file))
|
|
for (const k in envConfig) {
|
|
process.env[k] = envConfig[k]
|
|
}
|
|
}
|
|
return {
|
|
server: {
|
|
port: process.env.VITE_CLI_PORT,
|
|
proxy: {
|
|
// 把key的路径代理到target位置
|
|
// detail: https://cli.vuejs.org/config/#devserver-proxy
|
|
[process.env.VITE_BASE_API]: { // 需要代理的路径 例如 '/api'
|
|
target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
|
|
changeOrigin: true,
|
|
rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),
|
|
}
|
|
},
|
|
},
|
|
plugins: [
|
|
vuePlugin(),
|
|
AutoImport({resolvers: [VantResolver()]}),
|
|
Components({resolvers: [VantResolver()]})
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url))
|
|
}
|
|
}
|
|
}
|
|
}) |