`resolve.alias` with `isSsrBuild` only works at build time — during `npm run dev`, `isSsrBuild` is always false, so `ssrLoadModule` was loading the browser stub (DatabaseContext = null) instead of the real AsyncLocalStorage implementation, causing a TypeError on every request. Replace the alias with an `enforce: 'pre'` plugin that checks `options.ssr` at resolve time, which Vite correctly sets for both dev SSR module loading and production SSR builds. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
37 lines
970 B
TypeScript
37 lines
970 B
TypeScript
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(({ isSsrBuild }) => ({
|
|
build: {
|
|
rollupOptions: 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(),
|
|
],
|
|
server: {
|
|
allowedHosts: true,
|
|
},
|
|
}));
|