import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
  plugins: [react(), cssInjectedByJsPlugin()],

  // Dev server — serves index.html → src/dev.tsx (SPA mode)
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
      },
    },
  },

  // Replace Node.js globals so the bundle runs in the browser.
  // React checks process.env.NODE_ENV to choose its production/dev build.
  define: {
    'process.env.NODE_ENV': JSON.stringify(mode),
    'process.env': '{}',
  },

  // Production build — library mode for Hub loading
  // Produces dist/main.js as a self-contained ESM module
  build: {
    lib: {
      entry: 'src/main.tsx',
      formats: ['es'],
      fileName: 'main',
    },
    cssCodeSplit: false,
  },
}));
