import { defineConfig, loadEnv } from 'vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'
import ViteComponents from 'unplugin-vue-components/vite'
import DefineOptions from 'unplugin-vue-define-options/vite'

import { resolve } from 'path'

// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  const proxy = {
    '/api': {
      target: 'https://test.4dkankan.com',
      changeOrigin: true,
      rewrite: path => path.replace(/^\/api/, '')
    }
  }

  return defineConfig({
    base: './',
    plugins: [
      vue(),
      DefineOptions(),
      ViteComponents({
        resolvers: [
          AntDesignVueResolver({ resolveIcons: true, importStyle: 'less' })
        ],
        dts: 'src/components.d.ts'
      })
    ],
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          modifyVars: {
            'primary-color': '#0076F6',
            'link-color': '#0076F6',
            'border-radius-base': '2px'
          }
        }
      }
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, './src')
        }
      ]
    },
    server: {
      host: '0.0.0.0',
      port: 5173,
      open: true,
      proxy: proxy
    }
  })
}
