Skip to content

Write HTML. Render video. Built for agents.

fix(producer): preserve nested entry asset base (#3645)

Miguel Ángel32f9ed988000
diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts
index 0981973132..804b6bcc2d 100644
--- a/packages/producer/src/services/htmlCompiler.test.ts
+++ b/packages/producer/src/services/htmlCompiler.test.ts
@@ -653,6 +653,32 @@ describe("detectRenderModeHints", () => {
}
});
+ it("rebases a direct nested entry's sibling assets without changing project-root assets", async () => {
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-direct-entry-base-"));
+ const compositionsDir = join(projectDir, "compositions");
+ mkdirSync(compositionsDir, { recursive: true });
+ writeFileSync(join(compositionsDir, "sibling.png"), "sibling");
+ writeFileSync(join(projectDir, "root.png"), "root");
+ const entryPath = join(compositionsDir, "scene.html");
+ writeFileSync(
+ entryPath,
+ `<!doctype html><html><body>
+ <div data-composition-id="scene" data-width="100" data-height="100" data-duration="1">
+ <img id="sibling" src="sibling.png" />
+ <img id="root" src="root.png" />
+ </div>
+</body></html>`,
+ );
+
+ const compiled = await compileForRender(projectDir, entryPath, projectDir);
+ const { document } = parseHTML(compiled.html);
+
+ expect(document.querySelector("#sibling")?.getAttribute("src")).toBe(
+ "compositions/sibling.png",
+ );
+ expect(document.querySelector("#root")?.getAttribute("src")).toBe("root.png");
+ });
+
// Shared fixture builder for the assertSubCompositionsUsable / EmptyCompositionError
// pre-flight tests below. `subCompFiles` is a map of compositions/-relative
// filename to raw file content (empty string / malformed text / valid HTML).
diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts
index ce0f264ed0..004e443ce1 100644
--- a/packages/producer/src/services/htmlCompiler.ts
+++ b/packages/producer/src/services/htmlCompiler.ts
@@ -11,7 +11,7 @@
*/
import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs";
-import { join, dirname, resolve, basename } from "path";
+import { join, dirname, resolve, basename, relative } from "path";
import { parseHTML } from "linkedom";
import {
compileTimingAttrs,
@@ -25,6 +25,9 @@ import {
readMediaStart,
redactTelemetryString,
resolveNaturalMediaTimelineDurationFromValues,
+ rewriteAssetPaths,
+ rewriteCssAssetUrls,
+ rewriteInlineStyleAssetUrls,
type ResolvedDuration,
type UnresolvedElement,
} from "@hyperframes/core";
@@ -1865,6 +1868,38 @@ function rewriteUnresolvableGsapToCdn(html: string, projectDir: string): string
);
}
+/**
+ * Preserve a nested entry document's browser URL base when compilation moves
+ * it to compiled/index.html. Use the same sibling-first/project-root-fallback
+ * rule as mounted sub-compositions so both supported entry modes agree.
+ */
+function rebaseDirectEntryAssetPaths(html: string, projectDir: string, htmlPath: string): string {
+ if (!isPathInside(htmlPath, projectDir)) return html;
+ const entryPath = relative(projectDir, htmlPath).replace(/\\/g, "/");
+ if (!entryPath.includes("/")) return html;
+
+ const { document } = parseHTML(html);
+ const assetExists = (path: string) => existsSync(resolve(projectDir, path));
+ rewriteAssetPaths(
+ document.querySelectorAll("[src], [href]"),
+ entryPath,
+ (el: Element, attr: string) => el.getAttribute(attr),
+ (el: Element, attr: string, value: string) => el.setAttribute(attr, value),
+ assetExists,
+ );
+ rewriteInlineStyleAssetUrls(
+ document.querySelectorAll("[style]"),
+ entryPath,
+ (el: Element) => el.getAttribute("style"),
+ (el: Element, value: string) => el.setAttribute("style", value),
+ assetExists,
+ );
+ for (const style of document.querySelectorAll("style")) {
+ style.textContent = rewriteCssAssetUrls(style.textContent || "", entryPath, assetExists);
+ }
+ return document.toString();
+}
+
/**
* Compile an HTML composition project into a single self-contained HTML string
* with all media metadata resolved.
@@ -1876,7 +1911,12 @@ export async function compileForRender(
downloadDir: string,
options: CompileForRenderOptions = {},
): Promise<CompiledComposition> {
- const rawHtml = rewriteUnresolvableGsapToCdn(readFileSync(htmlPath, "utf-8"), projectDir);
+ const entryHtml = rebaseDirectEntryAssetPaths(
+ readFileSync(htmlPath, "utf-8"),
+ projectDir,
+ htmlPath,
+ );
+ const rawHtml = rewriteUnresolvableGsapToCdn(entryHtml, projectDir);
// Pre-flight: every data-composition-src reference must resolve to a
// usable file before we spend any time compiling, launching a browser, or