import { defineConfig } from "vite";
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import viteReact from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import { nitro } from "nitro/vite";

export default defineConfig(({ command, isPreview }) => ({
  server: {
    // 8080 is taken by sabnzbd on the host this deploys to, so dev uses Vite's
    // default instead.
    host: "127.0.0.1",
    port: 5173,
  },
  preview: {
    host: "127.0.0.1",
    port: 8479,
  },
  resolve: { tsconfigPaths: true },
  plugins: [
    tailwindcss(),
    tanstackStart(),
    // Nitro only during a build: left on in dev it opens a second dev-server
    // port and breaks HMR. `node-server` emits a plain .output/server/index.mjs
    // that systemd runs directly. `serverDir` is what auto-registers
    // server/middleware/book-api.ts — without it the app loses its storage API.
    ...(command === "build" || isPreview
      ? [nitro({ preset: "node-server", serverDir: "./server" })]
      : []),
    viteReact(),
  ],
}));
