import { defineConfig } from "vite";
import solid from "vite-plugin-solid";

/**
 * box3d-wasm ships two builds and picks one at runtime. The threaded "deluxe"
 * build needs SharedArrayBuffer, which browsers only expose on a cross-origin
 * isolated page — hence the COOP/COEP headers below. Without them we silently
 * fall back to the single-threaded build, which still works but leaves the
 * multithreaded solver on the table.
 */
const crossOriginIsolation = {
  "Cross-Origin-Opener-Policy": "same-origin",
  "Cross-Origin-Embedder-Policy": "require-corp",
};

export default defineConfig({
  plugins: [solid()],
  server: {
    headers: crossOriginIsolation,
    // Generated characters live in the workspace-root `assets/` directory, which
    // is outside the client package. One source of truth beats copying GLBs
    // into `public/` on every harness run.
    fs: { allow: [".."] },
  },
  preview: { headers: crossOriginIsolation },
  // The threaded build's pthread worker uses top-level await, which cannot be
  // expressed in Vite's default `iife` worker format.
  worker: { format: "es" },
  // Emscripten glue resolves its .wasm via `new URL(..., import.meta.url)`.
  // Pre-bundling rewrites that URL and breaks the fetch, so keep it external.
  optimizeDeps: { exclude: ["box3d-wasm"] },
  build: { target: "es2022" },
});
