Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
feat(core): add an inlineAssets option to skip base64 asset inlining (#4198)
Miguel Ángel038851c7a85d
diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.tsindex 4a77abd26f..90f57c52ef 100644--- a/packages/core/src/compiler/htmlBundler.test.ts+++ b/packages/core/src/compiler/htmlBundler.test.ts@@ -1132,6 +1132,17 @@ describe("bundleToSingleHtml", () => {expect(lutSrc).toBe("assets/luts/identity.cube");});+ it("inlineAssets: false also keeps a LUT path external, without inlineColorGradingLuts", async () => {+ const dir = makeColorGradingProject("assets/luts/identity.cube", {+ "assets/luts/identity.cube": "LUT_3D_SIZE 2",+ });++ const bundled = await bundleToSingleHtml(dir, { inlineAssets: false });+ const lutSrc = readBundledColorGradingLutSrc(bundled);++ expect(lutSrc).toBe("assets/luts/identity.cube");+ });+it("warns when a render bundle cannot inline a referenced color grading LUT", async () => {const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});try {@@ -1345,6 +1356,39 @@ describe("bundleToSingleHtml", () => {expect(bundled).not.toMatch(/["'(]assets\//);});+ it("keeps every asset's literal relative src when inlineAssets is false", async () => {+ const dir = makeTempProject({+ "index.html": `<!doctype html>+<html><head>+ <style>+ @font-face { font-family: "Brand"; src: url('assets/fonts/brand.woff2') format('woff2'); }+ .hero { background: url('assets/hero.jpg'); }+ </style>+</head><body>+ <div data-composition-id="root" data-width="320" data-height="180">+ <img id="avatar" src="assets/avatar-01.png" srcset="assets/[email protected] 2x">+ </div>+ <script>window.__timelines = window.__timelines || {}; window.__timelines.root = {}</script>+</body></html>`,+ "assets/fonts/brand.woff2": "font-bytes",+ "assets/hero.jpg": "hero-bytes",+ "assets/avatar-01.png": "avatar-bytes",+ "assets/[email protected]": "avatar-2x-bytes",+ });++ const bundled = await bundleToSingleHtml(dir, { inlineAssets: false });++ // The composition's own script can read `img.getAttribute("src")` back and+ // still find its authored path — this is what a same-origin asset route+ // (a sibling preview endpoint) needs to serve the real bytes.+ expect(bundled).toContain('src="assets/avatar-01.png"');+ expect(bundled).toContain("assets/[email protected] 2x");+ expect(bundled).toContain("url('assets/fonts/brand.woff2')");+ expect(bundled).toContain("url('assets/hero.jpg')");+ expect(bundled).not.toContain("data:image/png");+ expect(bundled).not.toContain("data:font/woff2");+ });+it("leaves an oversized asset relative and warns rather than inlining it", async () => {const dir = makeTempProject({"index.html": `<!doctype html>diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.tsindex f48ca0952d..3560e25f98 100644--- a/packages/core/src/compiler/htmlBundler.ts+++ b/packages/core/src/compiler/htmlBundler.ts@@ -348,7 +348,12 @@ function warnAssetTooLargeToInline(assetPath: string, byteLength: number): void);}-function maybeInlineRelativeAssetUrl(urlValue: string, projectDir: string): string | null {+function maybeInlineRelativeAssetUrl(+ urlValue: string,+ projectDir: string,+ inlineAssets: boolean,+): string | null {+ if (!inlineAssets) return null;if (!urlValue || !isRelativeUrl(urlValue)) return null;const { basePath, suffix } = splitUrlSuffix(urlValue.trim());if (!basePath) return null;@@ -401,7 +406,9 @@ function rewriteColorGradingLutWithInlinedAssets(value: string, projectDir: striconst lut = Reflect.get(parsed, "lut");if (typeof lut === "string") {- const inlined = maybeInlineRelativeAssetUrl(lut, projectDir);+ // Gated by inlineAssets and inlineColorGradingLuts at the call site above;+ // this call only runs once both have already passed.+ const inlined = maybeInlineRelativeAssetUrl(lut, projectDir, true);if (!inlined) {warnColorGradingLutNotInlined(lut);return value;@@ -412,7 +419,7 @@ function rewriteColorGradingLutWithInlinedAssets(value: string, projectDir: striif (typeof lut !== "object" || lut === null || Array.isArray(lut)) return value;const lutSrc = Reflect.get(lut, "src");if (typeof lutSrc !== "string") return value;- const inlined = maybeInlineRelativeAssetUrl(lutSrc, projectDir);+ const inlined = maybeInlineRelativeAssetUrl(lutSrc, projectDir, true);if (!inlined) {warnColorGradingLutNotInlined(lutSrc);return value;@@ -421,7 +428,11 @@ function rewriteColorGradingLutWithInlinedAssets(value: string, projectDir: strireturn JSON.stringify(parsed);}-function rewriteSrcsetWithInlinedAssets(srcsetValue: string, projectDir: string): string {+function rewriteSrcsetWithInlinedAssets(+ srcsetValue: string,+ projectDir: string,+ inlineAssets: boolean,+): string {if (!srcsetValue) return srcsetValue;return srcsetValue.split(",")@@ -430,19 +441,27 @@ function rewriteSrcsetWithInlinedAssets(srcsetValue: string, projectDir: string)if (!candidate) return candidate;const parts = candidate.split(/\s+/);if (parts.length === 0) return candidate;- const maybeInlined = maybeInlineRelativeAssetUrl(parts[0] ?? "", projectDir);+ const maybeInlined = maybeInlineRelativeAssetUrl(parts[0] ?? "", projectDir, inlineAssets);if (maybeInlined) parts[0] = maybeInlined;return parts.join(" ");}).join(", ");}-function rewriteCssUrlsWithInlinedAssets(cssText: string, projectDir: string): string {+function rewriteCssUrlsWithInlinedAssets(+ cssText: string,+ projectDir: string,+ inlineAssets: boolean,+): string {if (!cssText) return cssText;return cssText.replace(/\burl\(\s*(["']?)([^)"']+)\1\s*\)/g,(_full, quote: string, rawUrl: string) => {- const maybeInlined = maybeInlineRelativeAssetUrl((rawUrl || "").trim(), projectDir);+ const maybeInlined = maybeInlineRelativeAssetUrl(+ (rawUrl || "").trim(),+ projectDir,+ inlineAssets,+ );if (!maybeInlined) return _full;return `url(${quote || ""}${maybeInlined}${quote || ""})`;},@@ -767,6 +786,16 @@ export interface BundleOptions {* keeps showing project asset paths instead of giant data URLs.*/inlineColorGradingLuts?: boolean;+ /**+ * Inline fonts, raster images (img/href/poster/srcset/CSS url()) and color+ * grading LUTs as data URLs, up to the per-asset size ceiling. Default:+ * true, for a genuinely self-contained bundle. Set false when the caller+ * serves the project's own files alongside the bundle (e.g. a same-origin+ * asset route): assets then keep their authored relative URL, which the+ * caller resolves. `inlineColorGradingLuts` narrows LUTs further; it cannot+ * inline a LUT that this option has already excluded.+ */+ inlineAssets?: boolean;}/**@@ -1194,6 +1223,7 @@ export async function bundleToSingleHtml(injectTextRenderingRule(document);// Inline textual assets+ const inlineAssets = options?.inlineAssets !== false;for (const el of [...document.querySelectorAll("[src], [href], [poster], [xlink\\:href]")]) {for (const attr of ["src", "href", "poster", "xlink:href"] as const) {const value = el.getAttribute(attr);@@ -1203,24 +1233,29 @@ export async function bundleToSingleHtml(// origin and triggers "Unsafe attempt to load URL ... from frame".// Keep the project-relative URL; render/check servers already expose it.if (isExternalSvgFragmentUse(el, attr, value)) continue;- const inlined = maybeInlineRelativeAssetUrl(value, projectDir);+ const inlined = maybeInlineRelativeAssetUrl(value, projectDir, inlineAssets);if (inlined) el.setAttribute(attr, inlined);}}for (const el of [...document.querySelectorAll("[srcset]")]) {const srcset = el.getAttribute("srcset");- if (srcset) el.setAttribute("srcset", rewriteSrcsetWithInlinedAssets(srcset, projectDir));+ if (srcset)+ el.setAttribute("srcset", rewriteSrcsetWithInlinedAssets(srcset, projectDir, inlineAssets));}for (const styleEl of document.querySelectorAll("style")) {- styleEl.textContent = rewriteCssUrlsWithInlinedAssets(styleEl.textContent || "", projectDir);+ styleEl.textContent = rewriteCssUrlsWithInlinedAssets(+ styleEl.textContent || "",+ projectDir,+ inlineAssets,+ );}for (const el of [...document.querySelectorAll("[style]")]) {el.setAttribute("style",- rewriteCssUrlsWithInlinedAssets(el.getAttribute("style") || "", projectDir),+ rewriteCssUrlsWithInlinedAssets(el.getAttribute("style") || "", projectDir, inlineAssets),);}- if (options?.inlineColorGradingLuts !== false) {+ if (inlineAssets && options?.inlineColorGradingLuts !== false) {for (const el of [...document.querySelectorAll(`[${HF_COLOR_GRADING_ATTR}]`)]) {const value = el.getAttribute(HF_COLOR_GRADING_ATTR);if (value) {