#!/usr/bin/env node /** * Bundles the compiled CLI entry (dist/cli.js) into dist/bundle/ with esbuild. * * Why: the unbundled module graph is ~2,500 files; resolving and reading them * dominates startup (~1.5s on slow filesystems). The bundle loads the same code * from ~20 chunk files in under half the time. dist/ stays unbundled for * library consumers and type resolution; only the bin entry uses the bundle. * * Extension loading inside the bundle uses jiti virtualModules (same as the * compiled Bun binary), keyed off the __PI_BUNDLED__ define below, so extension * imports of pi packages share the bundle's module instances. */ import { chmodSync, readFileSync, rmSync } from "node:fs"; import { execFileSync } from "node:child_process"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { build } from "esbuild"; const packageDir = dirname(dirname(fileURLToPath(import.meta.url))); const outdir = join(packageDir, "dist", "bundle"); let buildId; try { buildId = execFileSync("git", ["describe", "--tags", "--always", "--dirty"], { cwd: dirname(packageDir), encoding: "utf8", }).trim(); } catch { buildId = `release-${JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8")).version}`; } rmSync(outdir, { recursive: true, force: true }); const result = await build({ entryPoints: { cli: join(packageDir, "dist", "cli.js"), // The Node-only lazy loader uses a variable import that esbuild cannot discover. "amazon-bedrock": join(packageDir, "dist", "node", "amazon-bedrock.js"), }, outdir, bundle: true, metafile: true, splitting: true, format: "esm", platform: "node", // Native or interop-sensitive packages stay external; they resolve from // node_modules at runtime (and are loaded via createRequire/lazily anyway). external: [ "koffi", "undici", "@silvia-odwyer/photon-node", "@mariozechner/clipboard", // Preserve Node's CommonJS interop for the AWS SDK's lazy transport imports. "@earendil-works/pi-ai/bedrock-provider", ], define: { __PI_BUNDLED__: "true", __PI_BUILD_ID__: JSON.stringify(buildId) }, banner: { js: "import { createRequire as __piBundleCreateRequire } from 'node:module'; const require = __piBundleCreateRequire(import.meta.url);", }, logLevel: "warning", }); const bedrockOutput = Object.entries(result.metafile.outputs).find( ([path]) => resolve(path) === join(outdir, "amazon-bedrock.js"), )?.[1]; for (const name of ["streamBedrock", "streamSimpleBedrock"]) { if (!bedrockOutput?.exports.includes(name)) { throw new Error(`Bedrock bundle is missing the ${name} export`); } } chmodSync(join(outdir, "cli.js"), 0o755); console.log("bundled dist/cli.js -> dist/bundle/");