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.
This commit is contained in:
Michael Wang
2026-11-17 17:56:40 +00:00
committed by GitHub
parent 4f2eaaac46
commit 2248d9994b
2 changed files with 53 additions and 12 deletions
@@ -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("<h1>Scene</h1>").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<string, string> {
const nested = `${"<template>".repeat(depth)}<div data-composition-src="level-0.html"></div>${"</template>".repeat(depth)}`;
const files = fanOut(true);
files["index.html"] = rootMounting("wrapper.html", 1).replace("</body>", `${nested}</body>`);
files["wrapper.html"] = SCENE;
if (file === "wrapper.html") {
files["index.html"] = rootMounting("wrapper.html", 1);
files["wrapper.html"] = SCENE.replace("<h1>Scene</h1>", `<h1>Scene</h1>${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("<template></template>").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();
}
},
);
});
+16 -11
View File
@@ -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 `<template>` content, which a document query does not see. */
/** `templated` holds mounts inside templates. Reading `template.content` per level would clone each subtree again. */
function compositionSrcsIn(html: string): { mounts: string[]; templated: string[] } {
const { document } = parseHTML(html);
const mounts: string[] = [];
const templated: string[] = [];
const templates = [...document.querySelectorAll("template")];
for (let template = templates.pop(); template; template = templates.pop()) {
templated.push(...mountSrcsUnder(template.content));
templates.push(...template.content.querySelectorAll("template"));
const stack: Array<[Element, boolean]> = [...document.children]
.reverse()
.map((el) => [el, false]);
for (let next = stack.pop(); next; next = stack.pop()) {
const [el, inTemplate] = next;
const srcPath = el.getAttribute("data-composition-src");
if (srcPath && !isUnresolvedAssetPlaceholder(srcPath)) {
(inTemplate ? templated : mounts).push(srcPath);
}
const childrenInTemplate = inTemplate || el.localName === "template";
const children = [...el.children];
for (let i = children.length - 1; i >= 0; i -= 1)
stack.push([children[i]!, childrenInTemplate]);
}
return { mounts: mountSrcsUnder(document), templated };
return { mounts, templated };
}
function compositionPathProblem(