import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/

/**
 * @type {import('vite').UserConfig}
 */
export default defineConfig(({ mode }) => {
  const { VITE_API_URL, VITE_DOMAIN_URL, VITE_IMG_API_URL, VITE_IMG_DOMAIN_URL } = loadEnv(mode, process.cwd())
  console.log("VITE_IMG_API_URL>>>>", VITE_IMG_DOMAIN_URL);
  return {
    plugins: [
      vue(),
      Components({
        resolvers: [VantResolver()]
      })
    ],
    resolve: {
      // 配置路径别名
      alias: {
        '@': '/src',
      },
    },
    server: {
      host: true,
      proxy: {
        [VITE_API_URL]: {
          target: VITE_DOMAIN_URL,
          changeOrigin: true,
          ws: true,
          rewrite: path => path.replace(new RegExp(`^${VITE_API_URL}`), ''),
        },
        [VITE_IMG_API_URL]: {
          target: VITE_IMG_DOMAIN_URL,
          changeOrigin: true,
          ws: true,
          rewrite: path => path.replace(new RegExp(`^${VITE_IMG_API_URL}`), ''),
        }
      },
      hmr: {
        overlay: false
      }
    },
    build: {
      rollupOptions: {
        external: [
          "element-plus",
        ],
      },
    }
  }
})
