import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// 清除系统代理环境变量，避免本地开发时系统代理（VPN/Clash等）干扰 vite proxy
delete process.env.HTTP_PROXY
delete process.env.HTTPS_PROXY
delete process.env.http_proxy
delete process.env.https_proxy
delete process.env.ALL_PROXY
delete process.env.all_proxy
// 设置 NO_PROXY 确保本地连接不走代理
process.env.NO_PROXY = 'localhost,127.0.0.1,::1'
process.env.no_proxy = 'localhost,127.0.0.1,::1'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  
  // 开发环境代理目标，默认测试服务器
  const proxyTarget = env.VITE_API_BASE_URL || 'https://apiservertest.s1f.ren'
  
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    optimizeDeps: {
      include: ['monaco-editor']
    },
    server: {
      // 强制绑定 IPv4 回环地址，避免 TUN/VPN 代理模式拦截 IPv6 或通配地址
      host: '0.0.0.0',
      port: 5176,
      strictPort: false,
      // proxy 已关闭，前端直接请求后端地址（在 src/config/env.js 中配置）
      // proxy: {
      //   '/api': {
      //     target: proxyTarget,
      //     changeOrigin: true,
      //     secure: false
      //   },
      //   '/acs': {
      //     target: proxyTarget,
      //     changeOrigin: true,
      //     secure: false
      //   }
      // }
    },
    build: {
      rollupOptions: {
        output: {
          manualChunks: {
            'monaco-editor': ['monaco-editor']
          }
        }
      }
    }
  }
})