Files
hyperframes/packages/producer/build.mjs
T
Miguel Ángel 0bf15119f8 feat: font resolution pipeline — compositions capture and embed their own fonts (#1255)
Compositions are now self-contained: the compiler captures font files
and embeds them as woff2 data URIs, eliminating silent render-time
fallback when the render environment lacks the author's fonts.

Resolution order (each tier falls through to the next):
1. Existing @font-face → use as-is
2. Bundled alias (38 cross-platform mappings) → embed data URI
3. Google Fonts → fetch, cache, embed
4. Local system font → locate on OS, compress to woff2, embed
5. Local @font-face paths → read file, compress, inline as data URI
6. External CDN stylesheets → fetch CSS, extract @font-face, inline
7. Alias map fallback → closest bundled equivalent
8. Actionable error with guidance

Key changes:
- System font locator (macOS/Windows/Linux) with path-bounding and
  symlink defense (realpathSync + O_NOFOLLOW)
- woff2 compression via wawoff2 (WASM, cross-platform)
- Multi-weight/style variant capture with length-sorted token matching
- External stylesheet inlining with SSRF defense (assertPublicHttpsUrl,
  HTTPS-only, private-host blocking, 2MB cap, 4-concurrent limit)
- Studio auto-import via GET /fonts/file API + renderAliasFor() derived
  from shared FONT_ALIAS_MAP (no more hand-curated drift)
- failClosedFontFetch throws on unresolved fonts in distributed renders
- Single source of truth: @hyperframes/core/fonts/aliases
- system_font_will_alias lint rule (escalates to warning for distributed)
- Default to Inter + JetBrains Mono in templates and CSS reset
2026-06-07 17:52:19 -04:00

138 lines
4.7 KiB
JavaScript

#!/usr/bin/env node
/**
* Build script for @hyperframes/producer (public OSS package)
*
* Bundles src/server.ts → dist/public-server.js (standalone server).
*/
import { build } from "esbuild";
import { mkdirSync, rmSync } from "fs";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
rmSync("dist", { recursive: true, force: true });
mkdirSync("dist", { recursive: true });
const scriptDir = dirname(fileURLToPath(import.meta.url));
const workspaceAliasPlugin = {
name: "workspace-alias",
setup(build) {
build.onResolve({ filter: /^@hyperframes\/engine$/ }, () => ({
path: resolve(scriptDir, "../engine/src/index.ts"),
}));
build.onResolve({ filter: /^@hyperframes\/engine\/alpha-blit$/ }, () => ({
path: resolve(scriptDir, "../engine/src/utils/alphaBlit.ts"),
}));
build.onResolve({ filter: /^@hyperframes\/engine\/shader-transitions$/ }, () => ({
path: resolve(scriptDir, "../engine/src/utils/shaderTransitions.ts"),
}));
build.onResolve({ filter: /^@hyperframes\/core$/ }, () => ({
path: resolve(scriptDir, "../core/src/index.ts"),
}));
build.onResolve({ filter: /^@hyperframes\/core\/lint$/ }, () => ({
path: resolve(scriptDir, "../core/src/lint/index.ts"),
}));
},
};
await Promise.all([
build({
bundle: true,
platform: "node",
target: "node22",
format: "esm",
external: ["puppeteer", "esbuild", "postcss", "wawoff2"],
plugins: [workspaceAliasPlugin],
minify: false,
sourcemap: true,
entryPoints: ["src/index.ts"],
outfile: "dist/index.js",
}),
build({
bundle: true,
platform: "node",
target: "node22",
format: "esm",
external: ["puppeteer", "esbuild", "postcss", "wawoff2"],
plugins: [workspaceAliasPlugin],
minify: false,
sourcemap: true,
entryPoints: ["src/server.ts"],
outfile: "dist/public-server.js",
}),
// PNG decode + alpha-blit worker (hf#732 lever-4). Loaded by
// `pngDecodeBlitWorkerPool.createPngDecodeBlitWorkerPool` via
// `new Worker(<path>)`. Must be a separate entry point so the worker
// module is standalone and shares no parent module-graph state.
build({
bundle: true,
platform: "node",
target: "node22",
format: "esm",
external: ["puppeteer", "esbuild", "postcss", "wawoff2"],
plugins: [workspaceAliasPlugin],
minify: false,
sourcemap: true,
entryPoints: ["src/services/pngDecodeBlitWorker.ts"],
outfile: "dist/services/pngDecodeBlitWorker.js",
}),
// Shader-blend worker (hf#677 follow-up). Loaded by
// `shaderTransitionWorkerPool.createShaderTransitionWorkerPool` via
// `new Worker(<path>)`. Same bundling rationale as the
// `pngDecodeBlitWorker` entry above.
build({
bundle: true,
platform: "node",
target: "node22",
format: "esm",
external: ["puppeteer", "esbuild", "postcss", "wawoff2"],
plugins: [workspaceAliasPlugin],
minify: false,
sourcemap: true,
entryPoints: ["src/services/shaderTransitionWorker.ts"],
outfile: "dist/services/shaderTransitionWorker.js",
}),
// `@hyperframes/producer/distributed` subpath — the public distributed
// render primitives (plan / renderChunk / assemble). Bundled as a
// separate entry so adopters that don't need the in-process renderer
// (Lambda chunk workers, CDK constructs, thin orchestrators) can import
// only this surface and skip the rest of the producer's dependency tree.
build({
bundle: true,
platform: "node",
target: "node22",
format: "esm",
external: ["puppeteer", "esbuild", "postcss", "wawoff2"],
plugins: [workspaceAliasPlugin],
minify: false,
sourcemap: true,
entryPoints: ["src/distributed.ts"],
outfile: "dist/distributed.js",
}),
]);
// Copy core runtime artifacts so the producer can find them at dist/
import { copyFileSync, existsSync, readFileSync } from "fs";
const coreDistDir = resolve(scriptDir, "../core/dist");
try {
const manifestSrc = resolve(coreDistDir, "hyperframe.manifest.json");
if (existsSync(manifestSrc)) {
copyFileSync(manifestSrc, "dist/hyperframe.manifest.json");
const manifest = JSON.parse(readFileSync(manifestSrc, "utf8"));
const runtimeIife = manifest?.artifacts?.iife || "hyperframe.runtime.iife.js";
copyFileSync(resolve(coreDistDir, runtimeIife), `dist/${runtimeIife}`);
console.log(`[Build] Copied runtime: hyperframe.manifest.json, ${runtimeIife}`);
}
} catch (e) {
console.warn("[Build] Warning: Could not copy runtime artifacts:", e.message);
}
// Generate .d.ts declarations (esbuild doesn't emit them)
import { execSync } from "child_process";
execSync("tsc --emitDeclarationOnly --declaration --declarationMap", {
stdio: "inherit",
});
console.log("[Build] Complete: dist/index.js, dist/public-server.js, *.d.ts");