fix(build): copy schema.json to dist and externalize zod (#534)
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)
This commit is contained in:
parent
1ea1e9ef43
commit
79744afd41
1 changed files with 15 additions and 5 deletions
20
build.bun.ts
20
build.bun.ts
|
|
@ -1,9 +1,15 @@
|
||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
import { $ } from "bun";
|
import { $ } from "bun";
|
||||||
|
import { cpSync, mkdirSync } from "node:fs";
|
||||||
|
|
||||||
// Run TypeScript compiler for type declarations
|
// Run TypeScript compiler for type declarations
|
||||||
await $`tsc`;
|
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";
|
const isDevelopment = Bun.env.NODE_ENV === "development";
|
||||||
|
|
||||||
// Build all JavaScript/TypeScript files
|
// 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([
|
await Promise.all([
|
||||||
buildJs("src/app.ts", {
|
buildJs("src/app.ts", {
|
||||||
outdir: "dist/src",
|
outdir: "dist/src",
|
||||||
external: ["@modelcontextprotocol/sdk"],
|
external: PEER_EXTERNALS,
|
||||||
}),
|
}),
|
||||||
buildJs("src/app.ts", {
|
buildJs("src/app.ts", {
|
||||||
outdir: "dist/src",
|
outdir: "dist/src",
|
||||||
|
|
@ -36,19 +46,19 @@ await Promise.all([
|
||||||
}),
|
}),
|
||||||
buildJs("src/app-bridge.ts", {
|
buildJs("src/app-bridge.ts", {
|
||||||
outdir: "dist/src",
|
outdir: "dist/src",
|
||||||
external: ["@modelcontextprotocol/sdk"],
|
external: PEER_EXTERNALS,
|
||||||
}),
|
}),
|
||||||
buildJs("src/react/index.tsx", {
|
buildJs("src/react/index.tsx", {
|
||||||
outdir: "dist/src/react",
|
outdir: "dist/src/react",
|
||||||
external: ["react", "react-dom", "@modelcontextprotocol/sdk"],
|
external: ["react", "react-dom", ...PEER_EXTERNALS],
|
||||||
}),
|
}),
|
||||||
buildJs("src/react/index.tsx", {
|
buildJs("src/react/index.tsx", {
|
||||||
outdir: "dist/src/react",
|
outdir: "dist/src/react",
|
||||||
external: ["react", "react-dom", "@modelcontextprotocol/sdk"],
|
external: ["react", "react-dom", ...PEER_EXTERNALS],
|
||||||
naming: { entry: "react-with-deps.js" },
|
naming: { entry: "react-with-deps.js" },
|
||||||
}),
|
}),
|
||||||
buildJs("src/server/index.ts", {
|
buildJs("src/server/index.ts", {
|
||||||
outdir: "dist/src/server",
|
outdir: "dist/src/server",
|
||||||
external: ["@modelcontextprotocol/sdk"],
|
external: PEER_EXTERNALS,
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue