metatron-open-seo/vite.config.ts
Ben Senescu 1a74904b67
Cut worker isolate baseline memory: stub just-bash, lazy-load cheerio (#340)
Production OOM triage: every 'Worker exceeded memory limit' burst hits
unrelated cheap routes right after a deploy — the 128MB limit is per
isolate, and the main worker's eagerly-evaluated module graph is what
crowds it, not any single request.

- Alias just-bash to a throwing stub (worker never uses it): removes
  just-bash + turndown + @mixmark-io/domino from the bundle. The chain
  was pulled in eagerly by @cloudflare/think via the SamChatAgent
  re-export in src/server.ts; SAM only uses its own MCP tools.
- Disable Think's workspace bash tool on SamChatAgent so the stub is
  unreachable at runtime.
- Dynamic-import page-analyzer (cheerio) in the site-audit crawl step
  so it evaluates only when an audit runs, not in every isolate.
- Drop the turndown CJS alias workaround (#339): turndown is no longer
  in the graph at all.

Main eager server chunk: 14,434 kB -> 11,712 kB (-19%); cheerio's
503 kB now a lazy chunk.
2026-07-03 22:23:35 -04:00

79 lines
2.5 KiB
TypeScript

import { fileURLToPath } from "node:url";
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import { defineConfig, loadEnv } from "vite";
import tsConfigPaths from "vite-tsconfig-paths";
import viteReact from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import { cloudflare } from "@cloudflare/vite-plugin";
import { devtools } from "@tanstack/devtools-vite";
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const port = process.env.PORT
? Number(process.env.PORT)
: env.PORT
? Number(env.PORT)
: 3001;
const showDevtools = env.VITE_SHOW_DEVTOOLS !== "false";
const allowedHosts = [
env.ALLOWED_HOST,
env.BETTER_AUTH_URL ? new URL(env.BETTER_AUTH_URL).hostname : undefined,
].filter((host): host is string => Boolean(host));
const emitSourcemaps = env.POSTHOG_SOURCEMAPS === "true";
return {
resolve: {
alias: {
// TODO: Remove this workaround once @cloudflare/think stops eagerly
// importing just-bash at module init
// (https://github.com/cloudflare/agents/issues/1673).
//
// just-bash (plus its turndown → @mixmark-io/domino chain, ~30 MB of
// source) is only used by Think's workspace bash tool, which SAM
// disables — but the eager import drags it into the main worker's
// startup module graph, inflating every isolate's baseline heap
// toward the 128 MB limit (production OOM bursts on unrelated
// routes). Alias it to a throwing stub so it never ships.
"just-bash": fileURLToPath(
new URL("./src/server/lib/just-bash-stub.ts", import.meta.url),
),
},
},
envPrefix: [
"VITE_",
"AUTH_MODE",
"BYPASS_EMAIL_VERIFICATION",
"POSTHOG_PUBLIC_KEY",
"POSTHOG_HOST",
"TURNSTILE_SITE_KEY",
],
server: {
allowedHosts,
port,
},
preview: {
allowedHosts,
port,
},
build: {
sourcemap: emitSourcemaps,
outDir: emitSourcemaps ? "dist-sourcemaps" : "dist",
},
plugins: [
showDevtools
? devtools({
consolePiping: {
enabled: true,
levels: ["log", "warn", "error", "info", "debug"],
},
})
: null,
cloudflare({ inspectorPort: false, viteEnvironment: { name: "ssr" } }),
tsConfigPaths(),
tanstackStart(),
viteReact(),
tailwindcss(),
],
};
});