import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const apiTarget =
  process.env.VITE_DEV_API_PROXY_TARGET || "http://localhost:5012";

export default defineConfig({
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("node_modules/react-dom") || id.includes("node_modules/react/")) {
            return "react-vendor";
          }
          if (id.includes("node_modules/recharts")) {
            return "chart-vendor";
          }
        },
      },
    },
  },
  resolve: {
    alias: {
      axios: path.resolve(__dirname, "node_modules/axios"),
    },
  },
  server: {
    fs: {
      allow: [path.resolve(__dirname, "..")],
    },
    port: Number(process.env.VITE_DEV_PORT) || 5176,
    proxy: {
      "/api/ptw": {
        target: apiTarget,
        changeOrigin: true,
      },
    },
  },
});
