import { defineConfig, loadEnv } from 'vite'
import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'node:path'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import compressPlugin from 'vite-plugin-compression'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import vueDevTools from 'vite-plugin-vue-devtools';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  // 品牌由命令行传入（cross-env VITE_BRAND=xxx），先定品牌再选 env 目录，避免循环依赖
  const brand = process.env.VITE_BRAND || 'default'
  const envDir = resolve(process.cwd(), 'brand', brand, 'env')

  const env = loadEnv(mode, envDir, 'VITE')

  return {
    base: env.VITE_BASE,
    envDir,
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@brand-config': fileURLToPath(new URL(`./brand/${brand}/index.js`, import.meta.url))
      },
      extensions: ['.js', '.json', 'jsx', '.vue', '.ts'] // 使用路径别名时想要省略的后缀名，可以自己 增减
    },
    root: process.cwd(),
    assetsInclude: ['./src/assets'],
    plugins: [
      vue(), 
      AutoImport({
        imports: [
          'vue',
          {
            'naive-ui': [
              'useDialog',
              'useMessage',
              'useNotification',
              'useLoadingBar'
            ]
          }
        ]
      }),
      Components({
        resolvers: [NaiveUiResolver()]
      }),
      vueJsx({}), 
      compressPlugin(), 
      UnoCSS(),
      vueDevTools({
        launchEditor: 'trae',
      })
    ],
    define: {
      __APP_ENV__: env.APP_ENV
    },
    build: {
      chunkSizeWarningLimit: 1000
    }
  }
})
