import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { defineConfig } from 'vite';

export default ({ mode }: Record<string, unknown>) =>
  defineConfig({
    plugins: [
      vue(),
      Components({
        dts: true,
        resolvers: [AntDesignVueResolver({ importStyle: false, resolveIcons: true })],
      }),
    ],
    base: '/web-amr',
    envPrefix: 'ENV_',
    resolve: {
      alias: {
        asset: resolve(__dirname, 'src/assets/'),
        '@': resolve(__dirname, 'src/pages/'),
        '@api': resolve(__dirname, 'src/apis/'),
        '@common': resolve(__dirname, 'src/components/'),
        '@core': resolve(__dirname, 'src/services/'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: { api: 'modern-compiler' },
      },
    },
    build: {
      target: 'es2020',
      outDir: 'dist',
      sourcemap: false,
      minify: 'esbuild',
      chunkSizeWarningLimit: 2000,
    },
    esbuild: {
      drop: mode === 'production' ? ['console'] : [],
    },
    server: {
      port: 8888,
      host: true,
      proxy: {
        '/mocks/': { target: 'http://localhost:8888/web-amr' },
        '/api/': {
          target: 'http://192.168.189.206:8080/jeecg-boot',
          rewrite: (path) => path.replace(/^\/api/, ''),
          changeOrigin: true,
        },
        '/ws/': {
          target: 'ws://192.168.189.206:8080/jeecg-boot',
          rewrite: (path) => path.replace(/^\/ws/, ''),
          changeOrigin: true,
          ws: true,
        },
      },
    },
  });
