Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): theme build's built-in value index does not depend on doc order (#10288)

Utsav Ojha047cc30e4415
diff --git a/.changeset/theme-known-values-doc-order.md b/.changeset/theme-known-values-doc-order.md
new file mode 100644
index 0000000000..0f486b074e
--- /dev/null
+++ b/.changeset/theme-known-values-doc-order.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx theme build` no longer depends on the order it loads the core component docs. A parent doc that lists a sub-component by name only, such as `Avatar.doc.mjs` listing `AvatarStatusDot`, erased the values the sub-component's own doc declares when it loaded after that doc; when `Avatar.doc.mjs` loaded last, `avatar-status-dot` `variant:success` counted as a custom value. The build now merges every doc's entry for a component.
+
+@utsavo-afk
diff --git a/packages/cli/api/theme/build/build.known-values-order.test.mjs b/packages/cli/api/theme/build/build.known-values-order.test.mjs
new file mode 100644
index 0000000000..bfedee8165
--- /dev/null
+++ b/packages/cli/api/theme/build/build.known-values-order.test.mjs
@@ -0,0 +1,89 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The built-in value index must not depend on the order docs load in.
+ *
+ * A parent doc can list a sub-component by name only (`Avatar.doc.mjs` lists
+ * `{name: 'AvatarStatusDot'}`), while the sub-component's own doc declares its
+ * props. This file lists every directory in reverse name order, which forces
+ * the parent doc to load after the sub-component doc that declares the values.
+ */
+
+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {themeBuild} from './build.mjs';
+
+vi.mock('node:fs', async importOriginal => {
+ const actual = /** @type {typeof import('node:fs')} */ (
+ await importOriginal()
+ );
+ /** @param {string | {name: string}} entry */
+ const nameOf = entry => (typeof entry === 'string' ? entry : entry.name);
+ /** @type {typeof actual.readdirSync} */
+ const readdirSync = /** @type {any} */ (
+ (/** @type {any[]} */ ...args) =>
+ /** @type {any[]} */ (
+ /** @type {any} */ (actual.readdirSync)(...args)
+ ).sort((a, b) => nameOf(b).localeCompare(nameOf(a)))
+ );
+ return {...actual, default: {...actual, readdirSync}, readdirSync};
+});
+
+let tmpDir;
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-known-values-'));
+});
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+/**
+ * @param {string} name
+ * @param {string} body
+ */
+async function build(name, body) {
+ fs.writeFileSync(
+ path.join(tmpDir, `${name}.mjs`),
+ `export default {
+ name: '${name}',
+ tokens: {'--color-background-body': '#fff'},
+ ${body}
+ };\n`,
+ );
+ return themeBuild(`${name}.mjs`, {}, {cwd: tmpDir});
+}
+
+describe('a sub-component value its own doc declares is built in', () => {
+ it('the root styles it without generating augmentation', async () => {
+ const receipt = await build(
+ 'root-builtin',
+ `components: {'avatar-status-dot': {'variant:success': {color: 'red'}}},`,
+ );
+
+ expect(receipt?.type).toBe('theme.build');
+ expect(receipt?.data?.outputs?.variantsDts).toBeFalsy();
+ });
+
+ it('an adaptation rule restyles it without a root declaration', async () => {
+ const receipt = await build(
+ 'rule-builtin',
+ `adaptations: {
+ rules: [
+ {
+ when: {pointer: 'coarse'},
+ value: {
+ components: {
+ 'avatar-status-dot': {'variant:success': {color: 'red'}},
+ },
+ },
+ },
+ ],
+ },`,
+ );
+
+ expect(receipt?.type).toBe('theme.build');
+ expect(receipt?.data?.outputs?.variantsDts).toBeFalsy();
+ });
+});
diff --git a/packages/cli/api/theme/build/build.mjs b/packages/cli/api/theme/build/build.mjs
index da9bb38f9b..5c98693950 100644
--- a/packages/cli/api/theme/build/build.mjs
+++ b/packages/cli/api/theme/build/build.mjs
@@ -925,17 +925,28 @@ async function loadKnownValuesIndex() {
return result;
};
+ // A component can appear in more than one doc: its own doc declares its
+ // props, and a parent doc can list it by name only. Merge every entry, so
+ // the result does not depend on the order the docs load in.
/** @type {Record<string, Record<string, Set<string>>>} */
const valuesByOwner = {};
+ /**
+ * @param {string} name
+ * @param {any[]} props
+ */
+ const addOwnerValues = (name, props) => {
+ const owner = (valuesByOwner[targetName(name)] ??= {});
+ for (const [prop, values] of Object.entries(valuesFromProps(props))) {
+ owner[prop] = new Set([...(owner[prop] ?? []), ...values]);
+ }
+ };
for (const doc of docs) {
if (typeof doc?.name === 'string') {
- valuesByOwner[targetName(doc.name)] = valuesFromProps(doc.props);
+ addOwnerValues(doc.name, doc.props);
}
for (const component of /** @type {any[]} */ (doc?.components ?? [])) {
if (typeof component?.name === 'string') {
- valuesByOwner[targetName(component.name)] = valuesFromProps(
- component.props,
- );
+ addOwnerValues(component.name, component.props);
}
}
}