mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-30 04:12:52 +09:00
## What changes Uploaded fonts used through CSS variables now reach the published site. Previously Builder could render them, but publishing filtered them out and emitted no `@font-face` rules. This fixes root font variables used by body and heading tokens with either quote style. The asset manager also protects those uploaded fonts from being deleted as unused. Closes #6603 ## Implementation The published bundle follows variables used by `font-family`, parses their values as font stacks, and handles nested variables without looping on cycles. Asset usage now uses the same lookup, records each referring style, and counts every uploaded weight in a family. Replacing a font used through a variable is blocked because the variable value cannot be rewritten by the current asset replacement path. Visitors still download each font file that the page uses; this change makes those files available rather than falling back to system fonts. ## Checklist - [x] Reproduce missing font assets in a published project bundle. - [x] Keep uploaded fonts referenced by root CSS variables and heading tokens. - [x] Verify both single and double quotes; exclude unrelated fonts. - [x] Verify regression tests fail before the publishing fix and pass afterward. - [x] Keep fonts referenced through CSS variables out of the unused-assets list and block deletion. - [x] Protect every uploaded weight in a used family and block unsafe font replacement. - [x] Verify the asset-usage regression tests fail before each fix and pass afterward. - [x] Run targeted Builder, project-build, SDK CSS, and CLI prebuild tests. - [x] Run Builder, project-build, and CLI typechecks, lint, formatting, and diff checks. - [x] Review `docs/`: existing custom-font and CSS-variable guides already describe the supported behavior; no text change is needed. - [x] Check fixture inputs and outputs: no fixture contains font variables, so regeneration is not needed. - [x] Push the tested changes. - [x] Monitor required CI checks and fix any failures. - [x] Simplify font family collection and asset usage without changing behavior. - [x] Re-run focused tests, typechecks, and CI after the cleanup. The cleanup removes a second per-style deduplication map. The collector now reports each family once per declaration. Production code shrank by four net lines; the remaining callback and variable map preserve each style's usage and protect asset deletion and replacement. - [x] Fix the fixtures diff check after submodule setup moves to current main. - [x] Verify the check still catches generated files and edits inside the submodule. - [x] Push the CI fix and monitor both PR and push workflows. ## Verification - Before the publishing fix, both quote variants produced an empty font asset list in the published bundle. Afterward, both include Inter and Playfair Display while excluding an unused uploaded font. - Asset usage tests failed before the follow-up fix for both quote styles, repeated references, multiple uploaded faces in one family, and unsafe replacement. They now pass. - Project-build asset and style utility tests: 95 passed. - Builder bundle tests: 11 passed. - CLI prebuild tests: 141 passed. - Builder, project-build, and CLI typechecks: passed. - Changed-file lint, formatting, and `git diff --check`: passed. - Local agent evaluations were not run because they require separate approval. - CI: Lint PR, Check submodules, Visual regression, and Main workflow passed. One unrelated Form E2E test failed on its first run; shard 10 passed on rerun. - Cleanup checks: 95 project-build tests, 11 Builder bundle tests, and 141 CLI prebuild tests passed. Project-build, Builder, and CLI typechecks, changed-file lint, formatting, and diff checks passed. - Cleanup CI: Lint PR, Check submodules, Visual regression, and Main workflow passed; all ten Builder E2E shards passed. - CI fix: a local Git reproducer confirmed pointer movement passes while tracked changes in the parent or submodule fail. YAML parsing and `git diff --check` passed. The staging push workflow, Linux and Windows fixture jobs, and all PR checks passed.
546 lines
15 KiB
TypeScript
546 lines
15 KiB
TypeScript
import { expect, test, vi } from "vitest";
|
|
import {
|
|
createAssetResourceRequest,
|
|
createStructuredAssetQueryResourceBody,
|
|
type Asset,
|
|
} from "@webstudio-is/sdk";
|
|
import { loadResource } from "@webstudio-is/sdk/runtime";
|
|
import {
|
|
createAssetIndex,
|
|
createCanonicalAssetFileEntry,
|
|
createContentRuntimeArtifact,
|
|
} from "@webstudio-is/content-engine/compiler";
|
|
import { createGeneratedAssetResourceRuntime } from "@webstudio-is/content-engine/runtime";
|
|
import { createDocumentGraph } from "@webstudio-is/content-engine";
|
|
import type { StyleValue } from "@webstudio-is/css-engine";
|
|
import {
|
|
createImageAssetFixture,
|
|
createPublishedProjectBundleFixture,
|
|
createSerializedBuildFixture,
|
|
} from "@webstudio-is/protocol/fixtures";
|
|
import { migratePages } from "@webstudio-is/project-migrations/pages";
|
|
import { __testing__ } from "./canvas.server";
|
|
|
|
const {
|
|
addProjectMetadata,
|
|
createLoadPublishedProjectBundleByProjectId,
|
|
serializeProjectBundle,
|
|
} = __testing__;
|
|
type BundleBuild = Parameters<typeof serializeProjectBundle>[0]["build"];
|
|
|
|
const createBundleBuild = (
|
|
overrides: Partial<BundleBuild> = {}
|
|
): BundleBuild => {
|
|
const serializedBuild = createSerializedBuildFixture();
|
|
return {
|
|
...serializedBuild,
|
|
pages: migratePages(serializedBuild.pages),
|
|
breakpoints: [],
|
|
styles: [],
|
|
styleSources: [],
|
|
styleSourceSelections: [],
|
|
props: [],
|
|
instances: [],
|
|
dataSources: [],
|
|
resources: [],
|
|
deployment: undefined,
|
|
marketplaceProduct: {
|
|
category: "pageTemplates",
|
|
name: "Test product",
|
|
thumbnailAssetId: "asset-id",
|
|
author: "Author",
|
|
email: "author@example.com",
|
|
description: "Description",
|
|
},
|
|
projectSettings: { meta: {}, compiler: {} },
|
|
...overrides,
|
|
};
|
|
};
|
|
|
|
test("serializes dev builds into project bundles without requiring deployment", () => {
|
|
const build = createBundleBuild();
|
|
const imageAsset = createImageAssetFixture({ projectId: build.projectId });
|
|
const videoAsset: Asset = {
|
|
...imageAsset,
|
|
id: "video-asset",
|
|
name: "video.mp4",
|
|
type: "video",
|
|
format: "mp4",
|
|
};
|
|
|
|
const bundle = serializeProjectBundle({
|
|
build,
|
|
assets: [imageAsset, videoAsset],
|
|
});
|
|
|
|
expect(bundle.build.id).toBe(build.id);
|
|
expect(bundle.build.deployment).toBeUndefined();
|
|
expect(bundle.page.path).toBe("");
|
|
expect(bundle.assets).toEqual([imageAsset, videoAsset]);
|
|
});
|
|
|
|
test("keeps font assets referenced from nested style values", () => {
|
|
const build = createBundleBuild({
|
|
styles: [
|
|
{
|
|
styleSourceId: "token",
|
|
breakpointId: "base",
|
|
property: "fontFamily",
|
|
value: {
|
|
type: "layers",
|
|
value: [
|
|
{
|
|
type: "fontFamily",
|
|
value: ["TokenFont", "sans-serif"],
|
|
},
|
|
],
|
|
} as unknown as StyleValue,
|
|
},
|
|
],
|
|
});
|
|
const fontAsset: Asset = {
|
|
id: "font-asset",
|
|
projectId: build.projectId,
|
|
name: "TokenFont.woff2",
|
|
type: "font",
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
format: "woff2",
|
|
size: 100,
|
|
meta: { family: "TokenFont", style: "normal", weight: 400 },
|
|
};
|
|
|
|
const bundle = serializeProjectBundle({
|
|
build,
|
|
assets: [fontAsset],
|
|
});
|
|
|
|
expect(bundle.assets).toEqual([fontAsset]);
|
|
});
|
|
|
|
test.each([
|
|
["double", '"Playfair Display", serif'],
|
|
["single", "'Playfair Display', serif"],
|
|
])(
|
|
"keeps fonts referenced through CSS variables with %s quotes",
|
|
(_quote, headingValue) => {
|
|
const build = createBundleBuild({
|
|
styles: [
|
|
{
|
|
styleSourceId: "root",
|
|
breakpointId: "base",
|
|
property: "--font-body",
|
|
value: { type: "unparsed", value: "Inter, system-ui, sans-serif" },
|
|
},
|
|
{
|
|
styleSourceId: "root",
|
|
breakpointId: "base",
|
|
property: "--font-heading",
|
|
value: { type: "unparsed", value: headingValue },
|
|
},
|
|
{
|
|
styleSourceId: "root",
|
|
breakpointId: "base",
|
|
property: "--font-unused",
|
|
value: { type: "unparsed", value: '"Unused Font", serif' },
|
|
},
|
|
{
|
|
styleSourceId: "body",
|
|
breakpointId: "base",
|
|
property: "fontFamily",
|
|
value: { type: "var", value: "font-body" },
|
|
},
|
|
{
|
|
styleSourceId: "heading-token",
|
|
breakpointId: "base",
|
|
property: "fontFamily",
|
|
value: { type: "var", value: "font-heading" },
|
|
},
|
|
],
|
|
});
|
|
const fontAsset = (id: string, family: string): Asset => ({
|
|
id,
|
|
projectId: build.projectId,
|
|
name: `${id}.woff2`,
|
|
type: "font",
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
format: "woff2",
|
|
size: 100,
|
|
meta: { family, style: "normal", weight: 400 },
|
|
});
|
|
const inter = fontAsset("inter", "Inter");
|
|
const playfair = fontAsset("playfair", "Playfair Display");
|
|
const unused = fontAsset("unused", "Unused Font");
|
|
|
|
const bundle = serializeProjectBundle({
|
|
build,
|
|
assets: [inter, playfair, unused],
|
|
});
|
|
|
|
expect(bundle.assets).toEqual([inter, playfair]);
|
|
}
|
|
);
|
|
|
|
test("validates collections when publication does not need a content index", async () => {
|
|
const build = createBundleBuild({
|
|
styles: [
|
|
{
|
|
styleSourceId: "token",
|
|
breakpointId: "base",
|
|
property: "fontFamily",
|
|
value: {
|
|
type: "layers",
|
|
value: [
|
|
{
|
|
type: "fontFamily",
|
|
value: ["UsedFont", "sans-serif"],
|
|
},
|
|
],
|
|
} as unknown as StyleValue,
|
|
},
|
|
],
|
|
});
|
|
const usedFont: Asset = {
|
|
id: "used-font",
|
|
projectId: build.projectId,
|
|
name: "UsedFont.woff2",
|
|
type: "font",
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
format: "woff2",
|
|
size: 100,
|
|
meta: { family: "UsedFont", style: "normal", weight: 400 },
|
|
};
|
|
const unusedFont: Asset = {
|
|
...usedFont,
|
|
id: "unused-font",
|
|
name: "UnusedFont.woff2",
|
|
meta: { ...usedFont.meta, family: "UnusedFont" },
|
|
};
|
|
const imageAsset = createImageAssetFixture({ projectId: build.projectId });
|
|
const currentAssetFolders = [{ id: "current-folder" }] as never;
|
|
const data = serializeProjectBundle({
|
|
build,
|
|
assets: [usedFont],
|
|
});
|
|
const validatePublishedAssetCollections = vi.fn().mockResolvedValue({
|
|
assets: [usedFont, unusedFont, imageAsset],
|
|
assetFolders: currentAssetFolders,
|
|
});
|
|
const preparePublishedAssetData = vi.fn();
|
|
const assetStore = {} as never;
|
|
|
|
const result = await addProjectMetadata(
|
|
data,
|
|
{
|
|
id: "project-id",
|
|
userId: null,
|
|
domain: "example.com",
|
|
title: "Example",
|
|
} as never,
|
|
{} as never,
|
|
{
|
|
getUserById: vi.fn(),
|
|
preparePublishedAssetData,
|
|
validatePublishedAssetCollections,
|
|
createAssetClient: vi.fn(() => assetStore),
|
|
}
|
|
);
|
|
|
|
expect(validatePublishedAssetCollections).toHaveBeenCalledWith({
|
|
projectId: "project-id",
|
|
context: {},
|
|
assetStore,
|
|
});
|
|
expect(preparePublishedAssetData).not.toHaveBeenCalled();
|
|
expect(result.assets).toEqual([usedFont, imageAsset]);
|
|
expect(result.assetFolders).toBe(currentAssetFolders);
|
|
});
|
|
|
|
test("build-id bundles do not read MDX bodies while preparing the RPC response", async () => {
|
|
const build = createBundleBuild();
|
|
const rootInstanceId = build.pages.pages.get(
|
|
build.pages.homePageId
|
|
)?.rootInstanceId;
|
|
if (rootInstanceId === undefined) {
|
|
throw new Error("Home page root is missing");
|
|
}
|
|
build.instances = [
|
|
{
|
|
type: "instance",
|
|
id: rootInstanceId,
|
|
component: "ws:block",
|
|
children: [],
|
|
},
|
|
];
|
|
build.props = [
|
|
{
|
|
id: "article-source",
|
|
instanceId: rootInstanceId,
|
|
name: "src",
|
|
type: "asset",
|
|
value: "article",
|
|
},
|
|
];
|
|
const article: Asset = {
|
|
...createImageAssetFixture({ projectId: build.projectId }),
|
|
id: "article",
|
|
name: "article.mdx",
|
|
type: "file",
|
|
format: "mdx",
|
|
meta: {},
|
|
};
|
|
const preparePublishedAssetData = vi.fn(() => {
|
|
throw new Error("RPC attempted to read article content");
|
|
});
|
|
const validatePublishedAssetCollections = vi.fn().mockResolvedValue({
|
|
assets: [article],
|
|
assetFolders: [],
|
|
});
|
|
const data = serializeProjectBundle({ build, assets: [article] });
|
|
const project = {
|
|
id: build.projectId,
|
|
userId: null,
|
|
domain: "example.com",
|
|
title: "Example",
|
|
} as never;
|
|
const dependencies = {
|
|
getUserById: vi.fn(),
|
|
preparePublishedAssetData,
|
|
validatePublishedAssetCollections,
|
|
createAssetClient: vi.fn(() => ({}) as never),
|
|
};
|
|
|
|
await expect(
|
|
addProjectMetadata(data, project, {} as never, dependencies)
|
|
).rejects.toThrow("RPC attempted to read article content");
|
|
preparePublishedAssetData.mockClear();
|
|
|
|
const result = await addProjectMetadata(
|
|
data,
|
|
project,
|
|
{} as never,
|
|
dependencies,
|
|
{ contentIndex: "client" }
|
|
);
|
|
|
|
expect(preparePublishedAssetData).not.toHaveBeenCalled();
|
|
expect(validatePublishedAssetCollections).toHaveBeenCalledOnce();
|
|
expect(result.assetIndex).toBeUndefined();
|
|
expect(result.assets).toContainEqual(article);
|
|
});
|
|
|
|
test.each([
|
|
{ origin: "https://project.wstd.io", destination: "saas" as const },
|
|
{ origin: "https://custom.example", destination: "saas" as const },
|
|
{ origin: "https://project.wstd.io", destination: "static" as const },
|
|
])(
|
|
"prepares Assets queries for $destination builds on $origin",
|
|
async ({ origin, destination }) => {
|
|
const source = "---\nslug: post\ntitle: Post\n---\nArticle body\n";
|
|
const article: Asset = {
|
|
...createImageAssetFixture(),
|
|
id: "article",
|
|
name: "article.md",
|
|
type: "file",
|
|
format: "md",
|
|
size: new TextEncoder().encode(source).byteLength,
|
|
meta: {},
|
|
};
|
|
const configuration = {
|
|
result: "one" as const,
|
|
where: {
|
|
field: ["properties", "slug"],
|
|
operator: "eq" as const,
|
|
value: "system.params.slug",
|
|
},
|
|
sort: [],
|
|
limit: "1",
|
|
offset: "0",
|
|
output: { mode: "all" as const, includeMetadata: false },
|
|
content: { mode: "markdown-body-ref" as const },
|
|
};
|
|
const build = createBundleBuild({
|
|
deployment:
|
|
destination === "saas"
|
|
? { destination, domains: [] }
|
|
: {
|
|
destination,
|
|
name: "site.zip",
|
|
assetsDomain: origin,
|
|
templates: ["ssg"],
|
|
},
|
|
resources: [
|
|
{
|
|
id: "article-query",
|
|
name: "Article",
|
|
control: "system",
|
|
method: "post",
|
|
url: '"/$resources/assets"',
|
|
headers: [],
|
|
body: createStructuredAssetQueryResourceBody(configuration),
|
|
},
|
|
],
|
|
dataSources: [
|
|
{
|
|
id: "article-data",
|
|
type: "resource",
|
|
name: "Article",
|
|
resourceId: "article-query",
|
|
},
|
|
],
|
|
});
|
|
const artifact = await createAssetIndex({
|
|
projectId: build.projectId,
|
|
entries: [
|
|
createCanonicalAssetFileEntry({
|
|
projectId: build.projectId,
|
|
document: {
|
|
_id: article.id,
|
|
_type: "asset.file",
|
|
name: article.name,
|
|
path: article.name,
|
|
key: "article",
|
|
extension: "md",
|
|
mimeType: "text/markdown",
|
|
size: article.size,
|
|
revision: "article-r1",
|
|
contentRef: article.name,
|
|
properties: { slug: "post", title: "Post" },
|
|
},
|
|
}),
|
|
],
|
|
documentGraph: createDocumentGraph({
|
|
nodes: [
|
|
{
|
|
id: article.id,
|
|
revision: "article-r1",
|
|
contentRef: article.name,
|
|
format: "markdown",
|
|
},
|
|
],
|
|
edges: [],
|
|
}),
|
|
});
|
|
const preparePublishedAssetData = vi.fn(async () => ({
|
|
artifact,
|
|
assets: [article],
|
|
assetFolders: [],
|
|
}));
|
|
const bundle = await addProjectMetadata(
|
|
serializeProjectBundle({ build, assets: [article] }),
|
|
{
|
|
id: build.projectId,
|
|
userId: null,
|
|
domain: "project.wstd.io",
|
|
title: "Example",
|
|
} as never,
|
|
{} as never,
|
|
{
|
|
getUserById: vi.fn(),
|
|
preparePublishedAssetData,
|
|
validatePublishedAssetCollections: vi.fn(async () => ({
|
|
assets: [article],
|
|
assetFolders: [],
|
|
})),
|
|
createAssetClient: vi.fn(() => ({}) as never),
|
|
},
|
|
{ contentIndex: "client" }
|
|
);
|
|
if (destination === "static") {
|
|
expect(preparePublishedAssetData).not.toHaveBeenCalled();
|
|
expect(bundle.assetIndex).toBeUndefined();
|
|
return;
|
|
}
|
|
const fetchDocument = vi.fn<typeof fetch>(async () => new Response(source));
|
|
// Match the generated runtime: a bundle without an index uses ordinary fetch.
|
|
const generatedFetch =
|
|
bundle.assetIndex === undefined
|
|
? fetch
|
|
: await createGeneratedAssetResourceRuntime({
|
|
deploymentId: build.id,
|
|
artifact: createContentRuntimeArtifact(bundle.assetIndex),
|
|
runtimeAssets: {
|
|
[article.id]: {
|
|
url: `/assets/${article.name}`,
|
|
contentRef: article.name,
|
|
},
|
|
},
|
|
})({
|
|
request: new Request(`${origin}/blog/post`),
|
|
fallback: fetchDocument,
|
|
});
|
|
const result = await loadResource(
|
|
generatedFetch,
|
|
createAssetResourceRequest({
|
|
query: {
|
|
...configuration,
|
|
where: { ...configuration.where, value: "post" },
|
|
limit: undefined,
|
|
offset: undefined,
|
|
},
|
|
})
|
|
);
|
|
expect(result).toMatchObject({
|
|
ok: true,
|
|
status: 200,
|
|
data: {
|
|
properties: { title: "Post" },
|
|
content: { text: "Article body\n" },
|
|
},
|
|
});
|
|
expect(preparePublishedAssetData).toHaveBeenCalledOnce();
|
|
expect(bundle.assetIndex?.contents).toBeUndefined();
|
|
expect(fetchDocument).toHaveBeenCalledOnce();
|
|
expect((fetchDocument.mock.calls[0][0] as Request).url).toBe(
|
|
`${origin}/assets/article.md`
|
|
);
|
|
}
|
|
);
|
|
|
|
test("loads project-id bundles from the published build", async () => {
|
|
const data = createPublishedProjectBundleFixture();
|
|
const project = {
|
|
id: "project-id",
|
|
latestBuildVirtual: { buildId: "published-build-id" },
|
|
} as never;
|
|
const context = { context: true } as never;
|
|
const loadProductionCanvasDataAndProject = vi.fn().mockResolvedValue({
|
|
data,
|
|
project,
|
|
});
|
|
const addProjectMetadata = vi.fn().mockResolvedValue(data);
|
|
const loadProjectById = vi.fn().mockResolvedValue(project);
|
|
const loadPublishedProjectBundleByProjectId =
|
|
createLoadPublishedProjectBundleByProjectId({
|
|
addProjectMetadata,
|
|
loadProductionCanvasDataAndProject,
|
|
loadProjectById,
|
|
});
|
|
|
|
await expect(
|
|
loadPublishedProjectBundleByProjectId("project-id", context)
|
|
).resolves.toBe(data);
|
|
|
|
expect(loadProjectById).toHaveBeenCalledWith("project-id", context);
|
|
expect(loadProductionCanvasDataAndProject).toHaveBeenCalledWith(
|
|
"published-build-id",
|
|
context,
|
|
project
|
|
);
|
|
expect(addProjectMetadata).toHaveBeenCalledWith(data, project, context);
|
|
});
|
|
|
|
test("rejects project-id bundle loads for unpublished projects", async () => {
|
|
const loadPublishedProjectBundleByProjectId =
|
|
createLoadPublishedProjectBundleByProjectId({
|
|
addProjectMetadata: vi.fn(),
|
|
loadProductionCanvasDataAndProject: vi.fn(),
|
|
loadProjectById: vi
|
|
.fn()
|
|
.mockResolvedValue({ latestBuildVirtual: null } as never),
|
|
});
|
|
|
|
await expect(
|
|
loadPublishedProjectBundleByProjectId("project-id", {} as never)
|
|
).rejects.toThrow("The project is not published yet");
|
|
});
|