import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { fileURLToPath, URL } from 'node:url';

export default defineConfig({
  plugins: [react()],
  resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
  server: {
    port: Number(process.env.PIG_DEMO_PORT ?? 5273),
    // `envs/` is outside the Vite root but inside the repo, and the reward
    // receipts import Python from it with `?raw`. Without this the dev server
    // refuses the read and the receipt panel renders empty.
    fs: { allow: ['..'] },
  },
  build: {
    outDir: 'dist',
    sourcemap: true,
    rollupOptions: {
      output: {
        manualChunks: {
          react: ['react', 'react-dom', 'react-router-dom'],
          // Named explicitly so the object form of manualChunks does not hoist
          // the chart library into the entry chunk. Only one tab imports it.
          charts: ['recharts'],
        },
      },
    },
  },
});
