Skip to content

Commit a81bfa1

Browse files
committed
fix(lint): Google Fonts links count too, and only for files the root mounts
1 parent cf43dd1 commit a81bfa1

3 files changed

Lines changed: 94 additions & 31 deletions

File tree

‎packages/lint/src/project.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,51 @@ describe("sub-composition stylesheet links", () => {
567567
expect(await fontFaceFindings(project)).toHaveLength(1);
568568
});
569569

570+
it("counts a Google Fonts link in the root for a sub-composition that uses the family", async () => {
571+
const project = makeProject(
572+
validHtml().replace(
573+
"<body>",
574+
'<head><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lobster+Two"></head><body>',
575+
),
576+
{
577+
"scene.html": `<template>
578+
<style>#scene-title{font-family:'Lobster Two'}</style>
579+
<div id="root" data-composition-id="scene" data-width="1920" data-height="1080" data-duration="1"><h1 id="scene-title">Hello</h1></div>
580+
<script>window.__timelines["scene"] = gsap.timeline({ paused: true });</script>
581+
</template>`,
582+
},
583+
);
584+
expect(await fontFaceFindings(project)).toEqual([]);
585+
});
586+
587+
it("counts a font a mounted composition declares, and ignores one only an unmounted file declares", async () => {
588+
const mount = (file: string) =>
589+
`<div id="${file}" data-composition-id="${file}" data-composition-src="compositions/${file}.html" data-start="0" data-duration="1"></div>`;
590+
const composition = (id: string, css: string) => `<template>
591+
<style>${css}</style>
592+
<div id="root" data-composition-id="${id}" data-width="1920" data-height="1080" data-duration="1"><h1 id="${id}-title">Hello</h1></div>
593+
<script>window.__timelines["${id}"] = gsap.timeline({ paused: true });</script>
594+
</template>`;
595+
const files = (declaring: string) => ({
596+
"scene.html": composition("scene", "#scene-title{font-family:'Repro'}"),
597+
[`${declaring}.html`]: composition(
598+
declaring,
599+
"@font-face{font-family:'Repro';src:local('Repro')}",
600+
),
601+
});
602+
const withHosts = (...hosts: string[]) =>
603+
validHtml().replace("</body>", `${hosts.join("")}</body>`);
604+
605+
expect(
606+
await fontFaceFindings(
607+
makeProject(withHosts(mount("scene"), mount("fonts")), files("fonts")),
608+
),
609+
).toEqual([]);
610+
expect(
611+
await fontFaceFindings(makeProject(withHosts(mount("scene")), files("unused"))),
612+
).toHaveLength(1);
613+
});
614+
570615
it("reads the stylesheet next to the sub-composition when both exist", async () => {
571616
const project = projectWithSharedFonts("compositions/frames", "fonts.css");
572617
writeFileSync(join(project, "fonts.css"), "@font-face{font-family:'Other';src:local('Other')}");

‎packages/lint/src/project.ts‎

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
lintVideoMediaStartPastEof,
4141
} from "./hevcPreviewLint.js";
4242
import { lintHyperframeHtml } from "./hyperframeLinter.js";
43-
import { extractFontFaceFamilies } from "./rules/fonts.js";
43+
import { collectGoogleFontFamilies, extractFontFaceFamilies } from "./rules/fonts.js";
4444
import { lintProjectShaderScripts } from "./projectShaders.js";
4545
import type {
4646
HyperframeLintFinding,
@@ -199,24 +199,30 @@ function resolveCssAssetCandidates(
199199
return resolveLocalAssetCandidates(projectDir, url);
200200
}
201201

202-
function collectHtmlFiles(compositionsDir: string): string[] {
203-
const walk = (dir: string, rel: string): string[] => {
204-
const out: string[] = [];
205-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
206-
const relPath = rel ? `${rel}/${entry.name}` : entry.name;
207-
if (entry.isDirectory()) {
208-
// Registry components are source snippets, not independently mounted
209-
// sub-compositions. Linting every installed template here makes an
210-
// unused component fail the assembled project check.
211-
if (!rel && entry.name === "components") continue;
212-
out.push(...walk(join(dir, entry.name), relPath));
213-
} else if (entry.isFile() && entry.name.endsWith(".html") && !entry.name.startsWith("._")) {
214-
out.push(relPath);
215-
}
202+
function mountedSources(
203+
projectDir: string,
204+
rootHtml: string,
205+
rootCompSrcPath?: string,
206+
): HtmlSource[] {
207+
const sources: HtmlSource[] = [{ html: rootHtml, compSrcPath: rootCompSrcPath }];
208+
const seen = new Set<string>();
209+
for (const source of sources) {
210+
const { document } = parseHTML(source.html);
211+
for (const host of querySelectorAllIncludingTemplates(document, "[data-composition-src]")) {
212+
const src = host.getAttribute("data-composition-src")?.trim();
213+
if (!src || isUnresolvedAssetPlaceholder(src) || isRemoteOrInlineUrl(src)) continue;
214+
const path = resolve(projectDir, src);
215+
if (seen.has(path) || !isWithinProjectRoot(projectDir, path)) continue;
216+
seen.add(path);
217+
const read = readProjectFile(path);
218+
if (read.kind !== "file") continue;
219+
sources.push({
220+
html: read.text,
221+
compSrcPath: relative(resolve(projectDir), path).replace(/\\/g, "/"),
222+
});
216223
}
217-
return out;
218-
};
219-
return walk(compositionsDir, "").sort();
224+
}
225+
return sources;
220226
}
221227

222228
function declaredProjectFontFamilies(projectDir: string, sources: HtmlSource[]): string[] {
@@ -230,6 +236,7 @@ function declaredProjectFontFamilies(projectDir: string, sources: HtmlSource[]):
230236
...collectLocalStylesheets(projectDir, document, compSrcPath),
231237
];
232238
for (const family of extractFontFaceFamilies(styles)) families.add(family);
239+
for (const family of collectGoogleFontFamilies(html, styles)) families.add(family);
233240
}
234241
return [...families];
235242
}
@@ -251,16 +258,10 @@ export async function lintProject(
251258
let totalInfos = 0;
252259

253260
const rootHtml = readFileSync(indexPath, "utf-8");
254-
const compositionsDir = resolve(projectDir, "compositions");
255-
const compositionFiles =
256-
!entryFile && existsSync(compositionsDir) ? collectHtmlFiles(compositionsDir) : [];
257-
const projectFontFamilies = declaredProjectFontFamilies(projectDir, [
258-
{ html: rootHtml, compSrcPath: rootCompSrcPath },
259-
...compositionFiles.map((file) => ({
260-
html: readFileSync(join(compositionsDir, file), "utf-8"),
261-
compSrcPath: `compositions/${file}`,
262-
})),
263-
]);
261+
const projectFontFamilies = declaredProjectFontFamilies(
262+
projectDir,
263+
mountedSources(projectDir, rootHtml, rootCompSrcPath),
264+
);
264265
const rootResult = await lintHyperframeHtml(rootHtml, {
265266
...hostOptions,
266267
filePath: indexPath,
@@ -274,8 +275,25 @@ export async function lintProject(
274275
totalInfos += rootResult.infoCount;
275276

276277
const allHtmlSources: HtmlSource[] = [{ html: rootHtml, compSrcPath: rootCompSrcPath }];
277-
if (compositionFiles.length > 0) {
278-
const files = compositionFiles;
278+
const compositionsDir = resolve(projectDir, "compositions");
279+
if (!entryFile && existsSync(compositionsDir)) {
280+
const collectHtmlFiles = (dir: string, rel: string): string[] => {
281+
const out: string[] = [];
282+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
283+
const relPath = rel ? `${rel}/${entry.name}` : entry.name;
284+
if (entry.isDirectory()) {
285+
// Registry components are source snippets, not independently mounted
286+
// sub-compositions. Linting every installed template here makes an
287+
// unused component fail the assembled project check.
288+
if (!rel && entry.name === "components") continue;
289+
out.push(...collectHtmlFiles(join(dir, entry.name), relPath));
290+
} else if (entry.isFile() && entry.name.endsWith(".html") && !entry.name.startsWith("._")) {
291+
out.push(relPath);
292+
}
293+
}
294+
return out;
295+
};
296+
const files = collectHtmlFiles(compositionsDir, "").sort();
279297
for (const file of files) {
280298
const filePath = join(compositionsDir, file);
281299
const html = readFileSync(filePath, "utf-8");

‎packages/lint/src/rules/fonts.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function extractGoogleFontFamiliesFromUrl(rawUrl: string): string[] {
167167
return families;
168168
}
169169

170-
function collectGoogleFontFamilies(
170+
export function collectGoogleFontFamilies(
171171
source: string,
172172
styles: Array<{ content: string }>,
173173
): Set<string> {

0 commit comments

Comments
 (0)