|
| 1 | +// Build entry — bundles the runtime to a single inlinable IIFE string and emits |
| 2 | +// the committed generated module that @leadbay/core imports. Mirrors the |
| 3 | +// promptforge emit pattern (writeIfDifferent + AUTO-GENERATED header; the |
| 4 | +// generated file lives in src/, not dist/, so it is committed source). |
| 5 | +// |
| 6 | +// Run via `tsx src/build.ts` (the package's `build` script). @leadbay/core's |
| 7 | +// `prebuild` runs this before its tsc so the generated string is fresh. |
| 8 | + |
| 9 | +import { build as esbuild } from "esbuild"; |
| 10 | +import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs"; |
| 11 | +import { dirname } from "node:path"; |
| 12 | +import { fileURLToPath } from "node:url"; |
| 13 | +import { VERSION } from "./runtime.js"; |
| 14 | + |
| 15 | +const RUNTIME_ENTRY = fileURLToPath(new URL("./runtime.ts", import.meta.url)); |
| 16 | +const USAGE_GUIDE = fileURLToPath(new URL("./usage-guide.md", import.meta.url)); |
| 17 | +const OUT = fileURLToPath( |
| 18 | + new URL("../../core/src/artifact-runtime.generated.ts", import.meta.url), |
| 19 | +); |
| 20 | + |
| 21 | +const HEADER = `// AUTO-GENERATED by @leadbay/components — do not edit directly. |
| 22 | +// Regenerate via: pnpm --filter @leadbay/components build |
| 23 | +// Source-of-truth: packages/components/src/{runtime.ts, usage-guide.md} |
| 24 | +
|
| 25 | +`; |
| 26 | + |
| 27 | +async function bundleRuntime(): Promise<string> { |
| 28 | + const result = await esbuild({ |
| 29 | + entryPoints: [RUNTIME_ENTRY], |
| 30 | + bundle: true, |
| 31 | + minify: true, |
| 32 | + format: "iife", |
| 33 | + // No globalName: the runtime self-attaches `window.LeadbayArtifacts` from |
| 34 | + // inside its module body (see runtime.ts), which is robust across script VMs |
| 35 | + // that don't expose esbuild's top-level `var` (jsdom, wrapped <script>). |
| 36 | + target: ["es2020"], |
| 37 | + legalComments: "none", |
| 38 | + write: false, |
| 39 | + }); |
| 40 | + return result.outputFiles[0].text.trim(); |
| 41 | +} |
| 42 | + |
| 43 | +function writeIfDifferent(path: string, content: string): boolean { |
| 44 | + mkdirSync(dirname(path), { recursive: true }); |
| 45 | + if (existsSync(path) && readFileSync(path, "utf8") === content) return false; |
| 46 | + writeFileSync(path, content, "utf8"); |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +async function main(): Promise<void> { |
| 51 | + const runtime = await bundleRuntime(); |
| 52 | + const usageGuide = readFileSync(USAGE_GUIDE, "utf8").trimEnd(); |
| 53 | + |
| 54 | + // JSON.stringify gives a safely-escaped TS string literal — no backtick or |
| 55 | + // `${` hazards from the minified runtime or the markdown. |
| 56 | + const module = |
| 57 | + HEADER + |
| 58 | + `export const ARTIFACT_KIT_VERSION = ${JSON.stringify(VERSION)};\n\n` + |
| 59 | + `/** Minified, self-contained IIFE. Inline once per artifact; it self-attaches\n` + |
| 60 | + ` * window.LeadbayArtifacts (lb.field / lb.action / lb.resource / lb.list / …).\n` + |
| 61 | + ` * Zero dependencies. */\n` + |
| 62 | + `export const ARTIFACT_RUNTIME: string = ${JSON.stringify(runtime)};\n\n` + |
| 63 | + `/** Markdown assembly recipe the agent reads to build an artifact. */\n` + |
| 64 | + `export const ARTIFACT_USAGE_GUIDE: string = ${JSON.stringify(usageGuide)};\n`; |
| 65 | + |
| 66 | + const rel = "packages/core/src/artifact-runtime.generated.ts"; |
| 67 | + |
| 68 | + // Drift guard: `build --check` verifies the committed generated module matches |
| 69 | + // a fresh build, and FAILS otherwise — so editing runtime.ts/usage-guide.md |
| 70 | + // without re-running the build can't silently ship a stale runtime/guide. |
| 71 | + if (process.argv.includes("--check")) { |
| 72 | + const current = existsSync(OUT) ? readFileSync(OUT, "utf8") : null; |
| 73 | + if (current !== module) { |
| 74 | + console.error( |
| 75 | + `[components] DRIFT: ${rel} is stale. Run \`pnpm --filter @leadbay/components build\` and commit the result.`, |
| 76 | + ); |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | + console.log(`[components] ${rel}: up to date`); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const changed = writeIfDifferent(OUT, module); |
| 84 | + console.log( |
| 85 | + `[components] ${rel}: ${changed ? "wrote" : "unchanged"} ` + |
| 86 | + `(runtime ${runtime.length}B, guide ${usageGuide.length}B)`, |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +main().catch((err) => { |
| 91 | + console.error("[components] build failed:", err); |
| 92 | + process.exit(1); |
| 93 | +}); |
0 commit comments