From 2248d9994b894c424f0f80dc984cb619a8e85d02 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 17 Nov 2026 17:56:40 +0000 Subject: [PATCH] fix(producer): scan nested templates for mounts in one linear walk (#8088) compositionSrcsIn read template.content at every nesting level. linkedom builds that fragment by deep-cloning the template's children, so a file of nested templates cost work and memory that grew with the square of the depth. The scan now walks the parsed tree once and tracks whether each element sits inside a template. --- .../services/htmlCompiler.sizeLimit.test.ts | 38 ++++++++++++++++++- .../producer/src/services/htmlCompiler.ts | 27 +++++++------ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index 974c3729aa..faea1d1ac5 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -1,7 +1,8 @@ import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { describe, expect, it } from "vitest"; +import { parseHTML } from "linkedom"; +import { describe, expect, it, vi } from "vitest"; import { assertCompositionFileSize, MAX_COMPOSITION_HTML_BYTES, @@ -154,4 +155,39 @@ describe("composition expansion budget", () => { const compiled = await compileForRender(dir, join(dir, "index.html"), dir); expect(compiled.html.split("

Scene

").length - 1).toBe(6); }); + + /** The fan-out project with `depth` nested templates in `file`, the innermost one mounting the fan-out. */ + function nestedFanOut(file: string, depth: number): Record { + const nested = `${"".repeat(depth)}`; + const files = fanOut(true); + files["index.html"] = rootMounting("wrapper.html", 1).replace("", `${nested}`); + files["wrapper.html"] = SCENE; + if (file === "wrapper.html") { + files["index.html"] = rootMounting("wrapper.html", 1); + files["wrapper.html"] = SCENE.replace("

Scene

", `

Scene

${nested}`); + } + return files; + } + + it.each([ + ["the entry file", "index.html"], + ["a mounted file", "wrapper.html"], + ])( + "finds mounts in deeply nested templates in %s without copying each level", + async (_, file) => { + const template = parseHTML("").document.querySelector("template")!; + const content = vi.spyOn(Object.getPrototypeOf(template), "content", "get"); + try { + const contentReads = async (depth: number) => { + content.mockClear(); + const error = await compileError(project(nestedFanOut(file, depth))); + expect(error).toMatchObject({ publicMetadata: { exceeded: "mounts" } }); + return content.mock.calls.length; + }; + expect(await contentReads(3_000)).toBe(await contentReads(1)); + } finally { + content.mockRestore(); + } + }, + ); }); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 0a6cd8449e..a13ad183cd 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -316,21 +316,26 @@ const isPrivateCompositionFile = (projectDir: string, filePath: string): boolean type CompositionProblem = { srcPath: string; detail: string }; -const mountSrcsUnder = (root: ParentNode): string[] => - [...root.querySelectorAll("[data-composition-src]")] - .map((el) => el.getAttribute("data-composition-src")) - .filter((srcPath): srcPath is string => !!srcPath && !isUnresolvedAssetPlaceholder(srcPath)); - -/** `templated` holds the mounts inside `