Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript

Build the API as a bundled Vercel function

Lewis Carhartf29df65d01d8
diff --git a/apps/api/public/.gitkeep b/apps/api/public/.gitkeep
deleted file mode 100644
index c4f50069..00000000
--- a/apps/api/public/.gitkeep
+++ /dev/null
@@ -1,5 +0,0 @@
-This directory is deliberately empty.
-
-The API ships no static assets, but a project with no framework preset
-expects an output directory to exist. Everything is served by the function
-in `api/`, which `vercel.json` rewrites all paths to.
diff --git a/apps/api/scripts/build-func.mjs b/apps/api/scripts/build-func.mjs
new file mode 100644
index 00000000..b07b3fbd
--- /dev/null
+++ b/apps/api/scripts/build-func.mjs
@@ -0,0 +1,188 @@
+/**
+ * Builds the API as a single Vercel function, via the Build Output API.
+ *
+ * Vercel's own tracing cannot deploy this app: the workspace packages export
+ * raw TypeScript (`@crm/auth` -> src/index.ts), and every relative import in
+ * `src` is extensionless because the repo compiles with
+ * `moduleResolution: "Bundler"`. Traced and run as-is, Node fails on one or
+ * the other — a missing `.ts`, or a missing `./create-app`.
+ *
+ * Bundling settles both at build time: bun resolves the whole graph, inlines
+ * the workspace source, and emits one ESM file with no runtime resolution
+ * left to do. Nothing about how the packages export has to change.
+ */
+import { execSync } from "node:child_process";
+import {
+ cpSync,
+ existsSync,
+ mkdirSync,
+ readdirSync,
+ readFileSync,
+ realpathSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const apiDir = dirname(dirname(fileURLToPath(import.meta.url)));
+const repoRoot = dirname(dirname(apiDir));
+const outDir = join(repoRoot, ".vercel/output");
+const funcDir = join(outDir, "functions/api/index.func");
+const bun = process.env.BUN_BIN || "bun";
+
+/**
+ * Left out of the bundle.
+ *
+ * The `@nestjs/*` entries are optional platforms Nest probes for at runtime
+ * and this app never installs; bundling them turns an absent peer into a
+ * build error. The rest are native or environment-specific modules that
+ * cannot survive being inlined.
+ */
+const EXTERNALS = [
+ "@nestjs/microservices",
+ "@nestjs/websockets",
+ "@nestjs/platform-fastify",
+ "@nestjs/graphql",
+ "@nestjs/swagger",
+ "class-transformer/storage",
+ "pg-native",
+ "better-sqlite3",
+ "cardinal",
+];
+
+/**
+ * Copied into the function's `node_modules` instead of bundled.
+ *
+ * Express is resolved by name at runtime by `@nestjs/platform-express`, so a
+ * bundled copy is invisible to it.
+ */
+const VENDOR_ROOTS = ["express"];
+
+rmSync(outDir, { recursive: true, force: true });
+mkdirSync(funcDir, { recursive: true });
+
+console.log("• bundling function with bun build...");
+execSync(
+ [
+ `${bun} build api/index.ts`,
+ "--target=node",
+ "--format=esm",
+ `--outfile=${JSON.stringify(join(funcDir, "index.mjs"))}`,
+ ...EXTERNALS.map((e) => `--external ${e}`),
+ ].join(" "),
+ { cwd: apiDir, stdio: "inherit" },
+);
+
+/**
+ * Vendoring walks bun's store rather than resolving by name across it.
+ *
+ * A store holds several versions of the same package side by side, so
+ * resolving by name alone can copy a version the dependent never asked for.
+ * Each `.bun/<pkg>@<ver>/node_modules/` entry links the exact versions that
+ * package resolves to, so dependencies are followed from there.
+ */
+console.log("• vendoring runtime-resolved dependencies...");
+const bunStore = join(repoRoot, "node_modules/.bun");
+const stores = existsSync(bunStore) ? readdirSync(bunStore) : [];
+const funcNm = join(funcDir, "node_modules");
+mkdirSync(funcNm, { recursive: true });
+let vendorCount = 0;
+
+function findInStore(name) {
+ const enc = name.replace(/\//g, "+");
+ for (const d of stores.filter((s) => s.startsWith(`${enc}@`))) {
+ const p = join(bunStore, d, "node_modules", name);
+ if (existsSync(p)) return p;
+ }
+ return null;
+}
+
+function packageVersion(dir) {
+ try {
+ return JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
+ } catch {
+ return null;
+ }
+}
+
+function resolveDep(realDir, name, depName) {
+ const storeNm = realDir.slice(0, realDir.length - name.length);
+ const candidate = join(storeNm, depName);
+ if (existsSync(candidate)) return realpathSync(candidate);
+ return findInStore(depName);
+}
+
+function place(realDir, dest) {
+ mkdirSync(dirname(dest), { recursive: true });
+ cpSync(realDir, dest, { recursive: true, dereference: true });
+ vendorCount++;
+}
+
+function vendorDeps(realDir, name, placedDir) {
+ let pj;
+ try {
+ pj = JSON.parse(readFileSync(join(realDir, "package.json"), "utf8"));
+ } catch {
+ return;
+ }
+ for (const depName of Object.keys(pj.dependencies || {})) {
+ const depReal = resolveDep(realDir, name, depName);
+ if (!depReal) {
+ console.warn(` ! not found: ${depName} (needed by ${name})`);
+ continue;
+ }
+ vendor(depName, depReal, placedDir);
+ }
+}
+
+function vendor(name, realDir, dependentDir) {
+ const rootDest = join(funcNm, name);
+ if (!existsSync(rootDest)) {
+ place(realDir, rootDest);
+ vendorDeps(realDir, name, rootDest);
+ return;
+ }
+ if (packageVersion(rootDest) === packageVersion(realDir)) return;
+ const nested = join(dependentDir, "node_modules", name);
+ if (existsSync(nested)) return;
+ place(realDir, nested);
+ vendorDeps(realDir, name, nested);
+}
+
+for (const root of VENDOR_ROOTS) {
+ const dir = findInStore(root);
+ if (!dir) {
+ console.warn(` ! not found: ${root}`);
+ continue;
+ }
+ vendor(root, realpathSync(dir), funcNm);
+}
+console.log(` vendored ${vendorCount} packages`);
+
+writeFileSync(join(funcDir, "package.json"), JSON.stringify({ type: "module" }));
+writeFileSync(
+ join(funcDir, ".vc-config.json"),
+ JSON.stringify({
+ runtime: "nodejs22.x",
+ handler: "index.mjs",
+ launcherType: "Nodejs",
+ shouldAddHelpers: false,
+ maxDuration: 60,
+ memory: 1769,
+ // Beside the database: Neon is us-east-1, and every request makes
+ // several round trips to it.
+ regions: ["iad1"],
+ }),
+);
+writeFileSync(
+ join(outDir, "config.json"),
+ JSON.stringify({
+ version: 3,
+ // Nest owns its own routing — /health, /api/auth/*, /api/trpc/* — so
+ // everything goes to the one function.
+ routes: [{ src: "/(.*)", dest: "/api/index" }],
+ }),
+);
+
+console.log(`✓ built ${outDir}`);
diff --git a/apps/api/vercel.json b/apps/api/vercel.json
deleted file mode 100644
index d324e413..00000000
--- a/apps/api/vercel.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "$schema": "https://openapi.vercel.sh/vercel.json",
- "functions": {
- "api/index.ts": {
- "includeFiles": "../../packages/**"
- }
- },
- "rewrites": [{ "source": "/(.*)", "destination": "/api" }]
-}