import { defineConfig, loadEnv, mergeConfig } from "vite"; import tailwindcss from "@tailwindcss/vite"; import tsConfigPaths from "vite-tsconfig-paths"; import viteReact from "@vitejs/plugin-react"; import { tanstackStart } from "@tanstack/react-start/plugin/vite"; import { devtools } from "@tanstack/devtools-vite"; export default defineConfig(async ({ command, mode }) => { const isDevBuild = command === "build" && mode === "development"; const loadedEnv = loadEnv(mode, process.cwd(), "VITE_"); const envDefine: Record = {}; for (const [key, value] of Object.entries(loadedEnv)) { envDefine[`import.meta.env.${key}`] = JSON.stringify(value); } const plugins = []; if (mode === "development") { plugins.push( devtools({ logging: false, eventBusConfig: { enabled: false }, enhancedLogs: { enabled: false }, consolePiping: { enabled: false }, removeDevtoolsOnBuild: false, injectSource: { enabled: true }, }), ); } plugins.push(tailwindcss()); plugins.push(tsConfigPaths({ projects: ["./tsconfig.json"] })); plugins.push( tanstackStart({ importProtection: { behavior: "error", client: { files: ["**/server/**"], specifiers: ["server-only"] }, }, // Redirect TanStack Start's bundled server entry to src/server.ts (our SSR error wrapper). // nitro/vite builds from this server: { entry: "server" }, }), ); if (command === "build") { const { nitro } = await import("nitro/vite"); plugins.push(nitro({ defaultPreset: "cloudflare-module" })); } plugins.push(viteReact()); let config = { define: envDefine, ...(isDevBuild ? { environments: { client: { define: { "process.env.NODE_ENV": JSON.stringify("development") } }, }, esbuild: { keepNames: true }, } : {}), css: { transformer: "lightningcss" as const }, resolve: { alias: { "@": `${process.cwd()}/src` }, dedupe: [ "react", "react-dom", "react/jsx-runtime", "react/jsx-dev-runtime", "@tanstack/react-query", "@tanstack/query-core", ], }, optimizeDeps: { include: [ "react", "react-dom", "react-dom/client", "react/jsx-runtime", "react/jsx-dev-runtime", ], ignoreOutdatedRequests: true, }, plugins, }; config = mergeConfig(config, { server: { host: "::", port: 8080, watch: { awaitWriteFinish: { stabilityThreshold: 1000, pollInterval: 100 } }, }, }); return config; });