import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { loadEnv } from 'vite'
import vueJsx from "@vitejs/plugin-vue-jsx";

export default defineConfig(({ command, mode }) => {

  const env = loadEnv(mode, process.cwd(), '');
  // console.log('env', env)
  return {
    base: '/',  // 设置相对路径
    define: {
      'process.env': env
    },
    server: {
      proxy: {
        '/api': {
          target: env.VITE_API_URL, //'http://172.18.0.3:8000/api',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '/open-platform')
        },
        '/open-platform': {
          target: env.VITE_API_URL, // 后端服务器地址
          changeOrigin: true, // 启用跨域
          // rewrite: (path) => path.replace(/^\/open-platform/, '') // 路径重写
        }
      },
      port: 8080,
      host: '0.0.0.0',
      open: true,
    },
    plugins: [
      vue(),
      vueJsx(),
      //vueDevTools(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
  }
})
