Skip to content

Commit 645c10a

Browse files
committed
fix(core): a font declared in a linked stylesheet no longer trips the bundler's font warning
1 parent 2d3033c commit 645c10a

5 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎packages/core/src/compiler/htmlBundler.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,26 @@ describe("bundleToSingleHtml", () => {
25892589
}
25902590
});
25912591

2592+
it("reads a font declared in a linked stylesheet, as hyperframes lint does", async () => {
2593+
const dir = makeTempProject({
2594+
"index.html": `<!doctype html><html><head><link rel="stylesheet" href="fonts.css"><style>#root { font-family: 'Hiragino Sans'; }</style></head><body>
2595+
<div id="root" data-composition-id="root" data-width="320" data-height="180" data-duration="1"></div>
2596+
<script>window.__timelines = {};</script>
2597+
</body></html>`,
2598+
"fonts.css": `@font-face { font-family: 'Hiragino Sans'; src: local('Hiragino Sans W3'); }`,
2599+
});
2600+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
2601+
try {
2602+
await bundleToSingleHtml(dir);
2603+
const guard = warnSpy.mock.calls
2604+
.map((call) => String(call[0] ?? ""))
2605+
.filter((line) => line.includes("[StaticGuard]"));
2606+
expect(guard).toEqual([]);
2607+
} finally {
2608+
warnSpy.mockRestore();
2609+
}
2610+
});
2611+
25922612
it("runs the StaticGuard lint unless staticGuard is false", async () => {
25932613
const dir = makeTempProject({
25942614
"index.html": `<!doctype html><html><head></head><body>

‎packages/core/src/compiler/htmlBundler.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,11 @@ async function bundleProject(projectDir: string, options?: BundleOptions): Promi
11041104
const compiled = await compileHtml(rawHtml, sourceDir, options?.probeMediaDuration);
11051105

11061106
if (options?.staticGuard !== false && !mountedShell) {
1107-
const staticGuard = await validateHyperframeHtmlContract(compiled);
1107+
const entryRel = relative(projectDir, indexPath).replace(/\\/g, "/");
1108+
const staticGuard = await validateHyperframeHtmlContract(compiled, {
1109+
dir: projectDir,
1110+
...(entryRel !== "index.html" && { compSrcPath: entryRel }),
1111+
});
11081112
if (!staticGuard.isValid) {
11091113
console.warn(
11101114
`[StaticGuard] Invalid HyperFrame contract: ${staticGuard.missingKeys.join("; ")}`,

‎packages/core/src/compiler/staticGuard.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lintHyperframeHtml } from "@hyperframes/lint";
1+
import { collectExternalStyles, lintHyperframeHtml } from "@hyperframes/lint";
22

33
export type HyperframeStaticFailureReason =
44
| "missing_composition_id"
@@ -13,10 +13,18 @@ export type HyperframeStaticGuardResult = {
1313
failureReason: HyperframeStaticFailureReason | null;
1414
};
1515

16+
/** `project` lets the lint read the stylesheets the html links, as `hyperframes lint` does; without it only inline
17+
* `<style>` is seen. `compSrcPath` is the html's project-relative path, unless it is the root index.html. */
1618
export async function validateHyperframeHtmlContract(
1719
html: string,
20+
project?: { dir: string; compSrcPath?: string },
1821
): Promise<HyperframeStaticGuardResult> {
19-
const result = await lintHyperframeHtml(html);
22+
const result = await lintHyperframeHtml(
23+
html,
24+
project && {
25+
externalStyles: collectExternalStyles(project.dir, html, project.compSrcPath),
26+
},
27+
);
2028
const missingKeys = result.findings
2129
.filter((finding) => finding.severity === "error")
2230
.map((finding) => finding.message);

‎packages/lint/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export {
1111
LINT_RULE_COUNT,
1212
LINT_RULE_GROUP_COUNTS,
1313
} from "./hyperframeLinter.js";
14-
export { lintProject, shouldBlockRender } from "./project.js";
14+
export { collectExternalStyles, lintProject, shouldBlockRender } from "./project.js";
1515
export type { ProjectLintResult } from "./project.js";

‎packages/lint/src/project.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function collectLocalStylesheets(
137137
return styles;
138138
}
139139

140-
function collectExternalStyles(
140+
export function collectExternalStyles(
141141
projectDir: string,
142142
html: string,
143143
compSrcPath?: string,

0 commit comments

Comments
 (0)