Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/render-class-aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[experimental] `astryx experimental export`: a large exported page that repeats an element names each repeated list of the StyleX classes Astryx's packages write (the `x…` classes) by one short class in its first paint, when those repeated lists add up to at least 16 KB; the page's own StyleX classes keep their names, so a large table or list writes a much smaller file: a 10,000-row grid goes from 52 MB to 18 MB. The page looks the same, also where scripts are blocked; its script draws with StyleX's own classes, and `--reverse` gives the page back exactly. The experimental `render()` takes `keepClassNames: true` for a caller that styles the file's markup with its own copy of the StyleX styles.

@josephfarina
7 changes: 7 additions & 0 deletions .changeset/render-image-once-markup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[experimental] `astryx experimental export`: an exported page's script reads from the first paint an image the first paint shows through `src`, `srcset` or `poster` when the page's code names it as a whole string by that exact path or https address. The file holds each such image one time fewer: a page with one 450 KB local photo goes from about 2.05 MB to 1.45 MB, and an https image the page shows through `src`, `srcset` or `poster` is held once (the same photo by an https address: about 1.46 MB to 0.86 MB). The first paint is the same, also where scripts are blocked.

@josephfarina
7 changes: 7 additions & 0 deletions .changeset/render-module-script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[experimental] `astryx experimental export`: an exported page's script is a module script when it runs the same in strict mode, so it runs once the file is parsed: a browser paints the page before the script draws it, and on the pages measured a small page shows its first paint about 200 ms sooner on a slow phone, with about the same time until it responds. A page whose script bundles a file that is not an ES module and does not ask for strict mode (CommonJS code, or a side-effect file with no `import` or `export`), or code that reads `document.currentScript`, keeps a classic script.

@josephfarina
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const ROUTES_LAZY = `document.addEventListener('click', e => {
e.preventDefault();
window.dispatchEvent(new Event('lazy'));
}, true);
export {};
`;

// A page that registers its own click and key handlers the ways pages do: one
Expand Down
11 changes: 9 additions & 2 deletions apps/docsite/scripts/generate-landing-template-previews.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ const templatesDir = path.resolve(
// high-density screen. It must be a width the image optimizer allows.
export const PREVIEW_IMAGE_WIDTH = 640;

/**
* How a preview whose markup the site takes is rendered: the site's own
* StyleX styles style it, so it keeps StyleX's class names instead of the
* short aliases whose rules only the file's own stylesheet holds.
*/
export const PREVIEW_MARKUP_RENDER = {keepClassNames: true};

// The page fills the frame and never scrolls, as a live template preview
// fills its tile: without this, a template taller than the frame shows a
// scrollbar where scrollbars are always visible. The card cannot run script
Expand All @@ -97,7 +104,7 @@ const SOURCE_BLOCK =
* The page's script, which the render puts after the embedded source. It
* holds no closing script tag of its own: the render escapes that.
*/
const PAGE_SCRIPT = /<script>[\s\S]*?<\/script>/u;
const PAGE_SCRIPT = /<script(?: type="module")?>[\s\S]*?<\/script>/u;

/**
* The page that the renderer renders for a tile: the template inside the
Expand Down Expand Up @@ -280,7 +287,7 @@ async function main() {
/** @type {Record<string, string>} */
const previews = {};
const renderPage = async (id, pageDir) => {
await render('page.tsx', {cwd: pageDir, out: 'page.html'});
await render('page.tsx', {cwd: pageDir, out: 'page.html', ...PREVIEW_MARKUP_RENDER});
previews[id] = previewMarkup(
fs.readFileSync(path.join(pageDir, 'page.html'), 'utf8'),
);
Expand Down
46 changes: 46 additions & 0 deletions apps/docsite/src/__tests__/landing-template-previews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {render} from '@astryxdesign/cli/api/experimental';
import {
PREVIEW_IMAGE_WIDTH,
PREVIEW_MARKUP_RENDER,
previewDocument,
previewMarkup,
previewPageSource,
Expand Down Expand Up @@ -143,6 +145,50 @@ describe('previewMarkup', () => {
expect(markup).not.toContain('x-astryx-source');
});

it("carries StyleX's own class names, which the site's styles match, for a page that repeats an element", async () => {
const dir = fs.mkdtempSync(path.join(docsiteDir, '.preview-classes-'));
try {
fs.writeFileSync(
path.join(dir, 'page.tsx'),
`import {Button} from '@astryxdesign/core/Button';
export default function Page() {
return <main>{Array.from({length: 400}, (_, i) => <Button key={i} label={\`Row \${i}\`} />)}</main>;
}
`,
);
await render('page.tsx', {
cwd: dir,
out: 'page.html',
...PREVIEW_MARKUP_RENDER,
});
const out = previewMarkup(
fs.readFileSync(path.join(dir, 'page.html'), 'utf8'),
);
const lists = [...out.matchAll(/<button[^>]*\sclass="([^"]*)"/g)].map(
m => m[1],
);
expect(lists).toHaveLength(400);
for (const list of lists) {
expect(list).toMatch(/\sx[0-9a-z]{5,}/);
expect(list).not.toMatch(/(^|\s)_a[0-9a-z]+(\s|$)/);
}
} finally {
fs.rmSync(dir, {recursive: true, force: true});
}
}, 120_000);

it("leaves out the page's script, a module script or a classic one", () => {
for (const tag of ['<script type="module">', '<script>']) {
const html = RENDERED.replace(
'</body>',
`${tag}document.title="drawn"</script></body>`,
);
const out = previewMarkup(html);
expect(out, tag).not.toContain('<script');
expect(out, tag).not.toContain('drawn');
}
});

it('keeps the image URLs of the rendered page and loads them at low priority', () => {
expect(markup).toContain(
'<img fetchpriority="low" decoding="async" src="/_next/image?url=%2Ftemplate-assets%2Flight-product-1.png&amp;w=640&amp;q=75" alt=""/>',
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/api/render/_adapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,12 @@ async function propValueWarnings(values, page) {

/**
* Render a page module to one HTML file and write it.
* @param {{cwd: string, input: string, output: string, title?: string, absolute?: boolean}} args absolute paths;
* `absolute`: the caller named the output by an absolute path
* @param {{cwd: string, input: string, output: string, title?: string, keepClassNames?: boolean, absolute?: boolean}} args absolute paths;
* `absolute`: the caller named the output by an absolute path;
* `keepClassNames`: keep every class of the first paint as written
* @returns {Promise<import('./render.type.mjs').RenderPageResponse['data']>}
*/
export async function renderToFile({cwd, input, output, title, absolute = false}) {
export async function renderToFile({cwd, input, output, title, keepClassNames = false, absolute = false}) {
readInput(input, cwd);
// An absolute --out is the caller's choice of place, also outside the
// current folder (cli-surface DEC-16). The file there is still never one a
Expand All @@ -583,7 +584,7 @@ export async function renderToFile({cwd, input, output, title, absolute = false}
try {
const set = servedSet(input);
try {
page = await renderPage(input, {title, version: cliVersion(), served: set && 'dir' in set ? set : null, output, cwd, cutFontFaces: true});
page = await renderPage(input, {title, version: cliVersion(), served: set && 'dir' in set ? set : null, output, cwd, cutFontFaces: true, keepClassNames});
} catch (e) {
// A page that needs the Core the CLI could not obtain says why.
const detail = /** @type {any} */ (e)?.code === 'ERR_RENDER_MISSING_DEPENDENCY' ? /** @type {any} */ (e).detail : null;
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/api/render/render.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export const doc = {
type: 'string',
description: 'Folder relative paths resolve from (default: the current directory).',
},
{
name: 'options.keepClassNames',
type: 'boolean',
description:
"Keep every class of the first paint as written, for a caller that takes the file's markup and styles it with its own copy of the StyleX styles. By default, once the repeated lists of the StyleX classes Astryx's packages write (the `x…` classes) add up to 16 KB, each such list becomes one short alias class, whose rules only the file's own stylesheet holds; the page's own StyleX classes keep their names. Rendering only; refused with `reverse`.",
default: 'false',
},
],
returns: [
{
Expand All @@ -89,7 +96,7 @@ export const doc = {
{code: 'ERR_MISSING_ARGUMENT', when: 'no file is given'},
{
code: 'ERR_INVALID_ARGUMENT',
when: 'the file or `out` has the wrong kind of extension, `out` is the input itself, `title` is given with `reverse`, or `overwrite` without it; with `reverse`, the page would land under a configuration name (`vite.config.ts`) or where a test, story or benchmark runner would run it (judged from the repository root)',
when: 'the file or `out` has the wrong kind of extension, `out` is the input itself, `title` or `keepClassNames` is given with `reverse`, or `overwrite` without it; with `reverse`, the page would land under a configuration name (`vite.config.ts`) or where a test, story or benchmark runner would run it (judged from the repository root)',
},
{code: 'ERR_FILE_NOT_FOUND', when: 'the file, or a local file the page imports, does not exist'},
{code: 'ERR_READ_FAILED', when: 'the file is there but cannot be read: no read permission, a folder or a pipe given as the file, or another file-system error; the message names the reason'},
Expand Down
Loading
Loading