fix: exclude database/context from browser bundle to fix Docker build (#2)
database/context.ts uses AsyncLocalStorage from node:async_hooks which is a Node.js-only API. Vite externalizes it for browser builds but the resulting error "AsyncLocalStorage is not exported by __vite-browser-external" caused npm run build to fail. Fix: add a Vite resolve.alias for the browser (non-SSR) build that maps ~/database/context to a browser-safe stub. The stub is never called at runtime since database() is only invoked in server-side loaders, but it allows the client bundle to build without errors. https://claude.ai/code/session_01Fjf9WFqNnuHmmC5yTedL7G Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
3210ab265f
commit
740ffbb411
2 changed files with 26 additions and 0 deletions
9
database/context.browser-stub.ts
Normal file
9
database/context.browser-stub.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// Browser-safe stub for database/context
|
||||
// The real implementation uses Node.js AsyncLocalStorage which is server-only.
|
||||
// This stub is used during the client build to avoid bundling server-only code.
|
||||
|
||||
export const DatabaseContext = null as any;
|
||||
|
||||
export function database(): never {
|
||||
throw new Error("database() can only be called on the server");
|
||||
}
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
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
|
||||
|
|
@ -11,6 +15,19 @@ export default defineConfig(({ isSsrBuild }) => ({
|
|||
}
|
||||
: undefined,
|
||||
},
|
||||
resolve: {
|
||||
alias: isSsrBuild
|
||||
? []
|
||||
: [
|
||||
{
|
||||
find: "~/database/context",
|
||||
replacement: path.resolve(
|
||||
__dirname,
|
||||
"database/context.browser-stub.ts"
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],
|
||||
server: {
|
||||
allowedHosts: true,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue