import react from '@vitejs/plugin-react';
import { defineConfig, loadEnv } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import { devShellPlugin } from './src/plugins/devShellPlugin';

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

  const backendUrl = env.VITE_BACKEND_URL;
  const proxyConfig = backendUrl
    ? {
        '/api': {
          target: backendUrl,
          changeOrigin: true,
          secure: false,
        },
      }
    : undefined;

  return {
    plugins: [tsconfigPaths({ root: __dirname }), react(), devShellPlugin()],
    define: {
      'process.env': {},
      'process.argv': [],
      'process.version': JSON.stringify(''),
      'process.platform': JSON.stringify(''),
    },
    optimizeDeps: {
      esbuildOptions: {
        loader: { '.js': 'jsx' },
      },
    },
    server: {
      host: true,
      proxy: proxyConfig,
    },
    build: {
      sourcemap: true,
      rollupOptions: {
        output: {
          manualChunks: {
            antd: ['antd'],
          },
        },
      },
    },
  };
});
