import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Icons from 'unplugin-icons/vite' // 图标插件
import IconsResolver from 'unplugin-icons/resolver' // 图标解析器
import Components from 'unplugin-vue-components/vite' // 组件自动导入
import { fileURLToPath, URL } from 'node:url'
import { resolve, dirname } from 'node:path'

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  
  const isProduction = mode === 'production'

  return defineConfig({
	base: isProduction ? '/cApi/' : '/',
    plugins: [
		vue(),
		Components({
		  resolvers: [
			// 自动解析图标组件（前缀为Icon，如<IconLogo />）
			IconsResolver({
			  prefix: 'Icon',
			}),
		  ],
		}),
		Icons({
		  compiler: 'vue3',
		  autoInstall: true, // 自动安装图标依赖
		}),
	],
    server: {
      proxy: {
        [env.VITE_BASE_API]: {
          target: env.VITE_SERVER_URL,
          changeOrigin: true,
        }
      }
    },
    resolve: {
      alias: {
        '@': resolve(dirname(fileURLToPath(import.meta.url)), 'src')
      }
    }
  })
}