/**
 * vite 配置文件
 */
import { defineConfig, loadEnv } from 'vite';
import { resolve } from 'path';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import PurgeIcons from 'vite-plugin-purge-icons';
import viteCompression from 'vite-plugin-compression';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import UnoCSS from 'unocss/vite';

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, __dirname, 'SHEEP_');
  console.log(env.SHEEP_PROXY_PREFIX);

  return {
    base: './',
    envPrefix: 'SHEEP_',
    plugins: [
      vue(),
      vueJsx(),
      UnoCSS(),
      viteCompression({ verbose: false }),
      createSvgIconsPlugin({
        iconDirs: [resolve(__dirname, './src/assets/svg')],
        symbolId: 'sa-[dir]-[name]',
      }),
      PurgeIcons(),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {},
      },
    },
    server: {
      host: true,
      open: true,
      port: env.SHEEP_DEV_PORT,
      hmr: {
        overlay: true,
      },
      // 动态代理配置
      proxy: {
        // 代理所有 API 请求
        [env.SHEEP_PROXY_PREFIX]: {
          target: env.SHEEP_PROXY_TARGET,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${env.SHEEP_PROXY_PREFIX}`), ''),
        },
      },
    },
    build: {
      chunkSizeWarningLimit: 2000,
      sourcemap: false,
    },
    preview: {
      port: env.SHEEP_PREVIEW_PORT,
    },
  };
});
