From cf0c85c128cfb28ec9580e0e6eee9d7e43479dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 7 Aug 2026 20:44:02 +0000 Subject: [PATCH] refactor(core): give composition assembly decisions one owner Mounting a composition and rendering one are two implementations of the same job. They answer the same questions -- which nodes are this composition's assets, in what order its scripts run, how its CSS is scoped, which head elements hoist, how nested sub-compositions are discovered, which element carries variable defaults -- and they answered one of them differently. Assets authored as siblings of the composition root were collected on render and dropped on mount, so three catalog components rendered unstyled in live preview while their video was correct. This adds the module those answers now live in. No behaviour changes yet; the next change routes both paths through it. It holds decisions only, never I/O. The two paths differ at their boundary in ways that are essential: Node with a linkedom document, synchronous, emitting strings on one side; a browser that fetches, mutates a live DOM and executes scripts on the other. The runtime also ships as an esbuild bundle to a CDN, so anything it can reach is weight and risk. Hence no imports at all, a structural input type rather than Document, and a test that asserts the import surface stays empty rather than a comment asking politely. The shape follows compositionScoping, which is already DOM-free and already imported by both sides. Four divergences surfaced while deriving the decisions, all currently live and none of them the reported bug: - the compiler drops inline scripts in a sub-composition head; it handles the src case and has no else branch, while the runtime executes them - link hoisting is conditional on render and unconditional on mount, so a templated sub-composition's webfont link is dropped in video and kept in preview - for a host that names no id, the compiler falls back to the first declared composition in the content and scopes to it; the runtime mounts the content whole, unflattened and unscoped - the compiler keeps two scope ids, one for CSS and one for scripts, so a script's self-referencing query resolves when a host names an id the content does not declare; the runtime uses one The module reports each in the shape the next change will need. Which side wins is a behaviour decision and is made there, not here. Verified: 17 tests, and the module's own defect reproduced by mutation -- collecting from the composition root instead of the whole template fails three of them, including the sibling-asset case. Core suite 1690 passing, typecheck:runtime and lint:runtime-preview-guards clean, oxlint and oxfmt clean. --- .../src/compiler/compositionAssembly.test.ts | 269 ++++++++++++++++++ .../core/src/compiler/compositionAssembly.ts | 250 ++++++++++++++++ 2 files changed, 519 insertions(+) create mode 100644 packages/core/src/compiler/compositionAssembly.test.ts create mode 100644 packages/core/src/compiler/compositionAssembly.ts diff --git a/packages/core/src/compiler/compositionAssembly.test.ts b/packages/core/src/compiler/compositionAssembly.test.ts new file mode 100644 index 0000000000..882b934f12 --- /dev/null +++ b/packages/core/src/compiler/compositionAssembly.test.ts @@ -0,0 +1,269 @@ +import { readFileSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; +import { + enumerateNestedCompositionHosts, + MAX_SUB_COMPOSITION_DEPTH, + planCompositionAssembly, + type CompositionAssemblyPlan, +} from "./compositionAssembly"; + +function parseComposition(html: string): Document { + return new DOMParser().parseFromString(html, "text/html"); +} + +/** + * Drive the module the way both real paths do: find the `