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 `