Skip to content

Write HTML. Render video. Built for agents.

2.5 KB
Raw Blame
/**
 * Bundle a stub entry into an injectable IIFE, wrapped as a TypeScript constant.
 *
 * Two artifacts are built this way — the audio-FX runtime and the position-edits
 * render — and the engine injects both into the headless browser as a script tag.
 * They were two copies of this file differing in five names, which is a poor
 * place for a divergence to hide: whichever copy stopped being edited would go on
 * producing a subtly different artifact with nothing to say so.
 */

import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { buildSync } from "esbuild";
import { formatGeneratedSource, writeGeneratedFile } from "./writeGeneratedFile.js";

export interface InjectedArtifact {
  /** The build script's own `import.meta.url`, so paths resolve beside it. */
  scriptUrl: string;
  /** Entry stub, relative to the package root. */
  entry: string;
  /** Output file name inside `src/generated`. */
  out: string;
  /** SCREAMING_CASE name for the string constant holding the IIFE. */
  constName: string;
  /** The accessor the rest of the codebase imports. */
  fnName: string;
  /** What that accessor returns, for its doc comment: "the pre-built X". */
  what: string;
  /** Structured log event name. */
  event: string;
}

export function buildInjectedArtifact(spec: InjectedArtifact): void {
  const scriptDir = dirname(fileURLToPath(spec.scriptUrl));
  const scriptName = spec.scriptUrl.split("/").pop() ?? "";
  const repoRoot = resolve(scriptDir, "..");
  const entry = resolve(repoRoot, spec.entry);
  const generatedDir = resolve(repoRoot, "src/generated");
  const outPath = resolve(generatedDir, spec.out);

  const result = buildSync({
    entryPoints: [entry],
    bundle: true,
    write: false,
    platform: "browser",
    format: "iife",
    target: ["es2020"],
    minify: true,
    legalComments: "none",
  });
  const iife = result.outputFiles[0]?.text ?? "";
  if (!iife) throw new Error(`esbuild produced no output for ${spec.entry.split("/").pop()}`);

  const source = [
    `// Generated by scripts/${scriptName}; do not edit.`,
    `const ${spec.constName}: string = ${JSON.stringify(iife)};`,
    "",
    `/** Returns the pre-built ${spec.what} as a string constant. */`,
    `export function ${spec.fnName}(): string {`,
    `  return ${spec.constName};`,
    "}",
    "",
  ].join("\n");

  writeGeneratedFile(outPath, formatGeneratedSource(source, outPath));

  console.log(JSON.stringify({ event: spec.event, outPath, bytes: iife.length }));
}