Eric Bower
·
2025-06-06
build.mts
1// https://github.com/colinhacks/zod/blob/c58bd9b0125881caee03a408eae88ec1dc5eb18b/packages/zod/build.mts
2import * as fs from "node:fs";
3import * as path from "node:path";
4import { execaSync } from "execa";
5
6const $ = execaSync({ stdio: "inherit" });
7
8$`rm -rf ./dist`;
9
10function writePackageJson(dir: string, fields: Record<string, any>) {
11 const packageJsonPath = path.join(path.resolve(dir), "package.json");
12 fs.mkdirSync(dir, {
13 recursive: true,
14 });
15 console.log(`writing package.json to ${packageJsonPath}...`);
16 fs.writeFileSync(packageJsonPath, JSON.stringify(fields, null, 2));
17 return packageJsonPath;
18}
19
20console.log("building ESM...");
21const esmPkg = writePackageJson("./src", { type: "module" });
22$`npx tsc -p tsconfig.esm.json`;
23fs.rmSync(esmPkg, { force: true });
24
25writePackageJson("./dist/esm", { type: "module" });
26
27console.log("building CJS...");
28const cjsPkg = writePackageJson("./src", { type: "commonjs" });
29$`npx tsc -p tsconfig.cjs.json`;
30fs.rmSync(cjsPkg, { force: true });
31writePackageJson("./dist/cjs", { type: "commonjs" });
32
33console.log("building types...");
34$`npx tsc -p tsconfig.types.json`;
35writePackageJson("./dist/types", { type: "commonjs" });
36
37console.log("DONE.");