55 lines · 1.8 KB
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import { cloudflare } from "@cloudflare/vite-plugin";
import { defineConfig } from "vite";
import viteReact from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";

/**
 * Local runs must never bind a deployed Artifacts namespace: an orphan sweep or a wiped local D1 would treat
 * the other side's repositories as orphans. `vite dev` (and `bun run preview`, which builds with
 * GITFOR_LOCAL=1) rebinds every Artifacts namespace to `<namespace>-dev`; `bun run deploy` builds without it.
 */
const DEV_NAMESPACE_SUFFIX = "-dev";

function devName(namespace: string): string {
  return namespace.endsWith(DEV_NAMESPACE_SUFFIX) ? namespace : `${namespace}${DEV_NAMESPACE_SUFFIX}`;
}

export default defineConfig(({ command, isPreview }) => {
  const local = (command === "serve" && !isPreview) || process.env.GITFOR_LOCAL === "1";
  return {
    server: {
      port: 3000,
      // The Worker answers CORS itself (e.g. the editor API's preflights from its opaque-origin frame).
      cors: false,
    },
    resolve: {
      tsconfigPaths: true,
    },
    plugins: [
      cloudflare({
        viteEnvironment: { name: "ssr" },
        ...(local
          ? {
              config: (config) => ({
                artifacts: (config.artifacts ?? []).map((binding) => ({
                  ...binding,
                  namespace: devName(binding.namespace),
                })),
                vars: {
                  ...config.vars,