From 4cc557cbc5ef04707bc458c988331e8deb42edf4 Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Fri, 6 Mar 2026 16:15:07 +0000 Subject: [PATCH] fix(build): copy schema.json to dist and externalize zod Two packaging bugs: 1. The './schema.json' export pointed to dist/src/generated/schema.json but nothing copied it there (tsc is emitDeclarationOnly, Bun.build doesn't emit JSON). Consumers got ERR_MODULE_NOT_FOUND. Fix: cpSync after tsc runs. 2. zod is a peerDependency but was bundled into every entry point (~270KB each). This causes dual-instance bugs where consumer-side 'instanceof ZodError' and schema.extend() break across boundaries. Fix: add zod to external list for all non-with-deps entries. Bundle size impact: app.js 293952 -> 24561 (-91.6%) app-bridge.js 299517 -> 30112 (-89.9%) react/index.js 295558 -> 26153 (-91.2%) server/index.js 291500 -> 22135 (-92.4%) app-with-deps 321814 -> 321814 (unchanged, intentional) --- build.bun.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/build.bun.ts b/build.bun.ts index 0e5341895..038103032 100644 --- a/build.bun.ts +++ b/build.bun.ts @@ -1,9 +1,15 @@ #!/usr/bin/env bun import { $ } from "bun"; +import { cpSync, mkdirSync } from "node:fs"; // Run TypeScript compiler for type declarations await $`tsc`; +// Copy schema.json (tsc is emitDeclarationOnly, Bun.build doesn't emit JSON assets). +// Needed for the "./schema.json" package export. +mkdirSync("dist/src/generated", { recursive: true }); +cpSync("src/generated/schema.json", "dist/src/generated/schema.json"); + const isDevelopment = Bun.env.NODE_ENV === "development"; // Build all JavaScript/TypeScript files @@ -25,10 +31,14 @@ function buildJs( }); } +// zod is a peerDependency — keep it external so consumers share a single +// zod instance (instanceof ZodError / schema.extend() break with duplicate copies). +const PEER_EXTERNALS = ["@modelcontextprotocol/sdk", "zod"]; + await Promise.all([ buildJs("src/app.ts", { outdir: "dist/src", - external: ["@modelcontextprotocol/sdk"], + external: PEER_EXTERNALS, }), buildJs("src/app.ts", { outdir: "dist/src", @@ -36,19 +46,19 @@ await Promise.all([ }), buildJs("src/app-bridge.ts", { outdir: "dist/src", - external: ["@modelcontextprotocol/sdk"], + external: PEER_EXTERNALS, }), buildJs("src/react/index.tsx", { outdir: "dist/src/react", - external: ["react", "react-dom", "@modelcontextprotocol/sdk"], + external: ["react", "react-dom", ...PEER_EXTERNALS], }), buildJs("src/react/index.tsx", { outdir: "dist/src/react", - external: ["react", "react-dom", "@modelcontextprotocol/sdk"], + external: ["react", "react-dom", ...PEER_EXTERNALS], naming: { entry: "react-with-deps.js" }, }), buildJs("src/server/index.ts", { outdir: "dist/src/server", - external: ["@modelcontextprotocol/sdk"], + external: PEER_EXTERNALS, }), ]);