import { sentryReactRouter } from "@sentry/react-router";
import { reactRouter } from "@react-router/dev/vite";
import tailwindcss from "@tailwindcss/vite";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig((env) => ({
  build: {
    rollupOptions: env.isSsrBuild
      ? {
          input: "./server/app.ts",
        }
      : undefined,
  },

  plugins: [{
    name: "database-context-alias",
    enforce: "pre" as const,
    resolveId(id, _importer, options) {
      if (id === "~/database/context") {
        return options?.ssr
          ? path.resolve(__dirname, "database/context.ts")
          : path.resolve(__dirname, "database/context.browser-stub.ts");
      }
    },
  }, tailwindcss(), reactRouter(), tsconfigPaths(), sentryReactRouter({
    org: "chris-parsons",
    project: "brackt",
    authToken: process.env.SENTRY_AUTH_TOKEN
  }, env)],

  server: {
    allowedHosts: true,
  },

  optimizeDeps: {
    exclude: ["@sentry/react-router"]
  }
}));
