import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'

// https://vite.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const servicePort = env.VITE_SERVICE_PORT || 300
  return {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        resolvers: [ElementPlusResolver()],
        dts: true
      }),
      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass' // 使用scss源码，支持自定义主题变量
          })
        ],
        dts: true
      }),
    ],
    server: {
      port: 5173,
      proxy: {
        '/api': {
          target: `http://localhost:${servicePort}`,
          changeOrigin: true
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 全局注入scss变量/混合，所有页面直接使用无需手动import
          additionalData: `@use "@/styles/vars.scss" as *;`
        }
      }
    }
  }
})
