import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'node:path';
import { visualizer } from 'rollup-plugin-visualizer';

export default defineConfig({
  plugins: [
    react(),
    visualizer({ open: false, filename: 'dist/stats.html' }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          'react-vendor': ['react', 'react-dom'],
        },
      },
    },
    chunkSizeWarningLimit: 1000,
  },
  server: {
    port: 5010,
    proxy: {
      '/api': {
        target: 'http://localhost:3010',
        changeOrigin: true
      },
      '/ws': {
        target: 'ws://localhost:3010',
        ws: true,
        changeOrigin: true,
        secure: false,
        configure: (proxy, _options) => {
          proxy.on('error', (err, _req, _res) => {
            // Silently handle WebSocket connection errors during development
            // These are common when connections are closed/reset and are non-fatal
            if (err.code !== 'ECONNRESET' && err.code !== 'ECONNABORTED') {
              console.error('WebSocket proxy error:', err);
            }
          });
        }
      }
    },
    hmr: {
      // Reduce HMR connection errors
      clientPort: 5010
    }
  }
});

