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 { ElementPlusResolver } from 'unplugin-vue-components/resolvers' export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), '') const allowedHosts = parseAllowedHosts(env.VITE_ALLOWED_HOSTS) return { plugins: [ vue(), AutoImport({ imports: ['vue', 'vue-router'], dts: 'src/auto-imports.d.ts', resolvers: [ElementPlusResolver({ importStyle: 'css' })], }), Components({ dts: 'src/components.d.ts', resolvers: [ElementPlusResolver({ importStyle: 'css' })], }), ], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), }, }, server: { host: env.VITE_DEV_HOST === 'true', allowedHosts: env.VITE_ALLOW_ALL_HOSTS === 'true' ? true : allowedHosts, proxy: { '/api': { target: env.VITE_API_TARGET || 'http://localhost:3000', changeOrigin: true, }, }, }, } }) function parseAllowedHosts(value?: string) { const hosts = value ?.split(',') .map((host) => host.trim()) .filter(Boolean) return hosts && hosts.length > 0 ? hosts : ['localhost', '127.0.0.1'] }