An open source design system that's fully customizable and agent ready
TypeScript
fix(api-report): leave StyleX-hashed custom properties out of the report (#9601)
Cindy Zhang447907497d17
diff --git a/scripts/api-report/__fixtures__/repo/packages/demo/lib/demo.css b/scripts/api-report/__fixtures__/repo/packages/demo/lib/demo.cssindex d75588dd81..a767e1123a 100644--- a/scripts/api-report/__fixtures__/repo/packages/demo/lib/demo.css+++ b/scripts/api-report/__fixtures__/repo/packages/demo/lib/demo.css@@ -4,6 +4,9 @@:root {--demo-accent: blue;--_demo-private: 1px;+ --x1ue0wla: 0px;+ --astryx-demo-offset: 2px;+ --xl: 24px;}.astryx-chip[data-astryx-theme] {--demo-gap: 4px;diff --git a/scripts/api-report/api-report.test.mjs b/scripts/api-report/api-report.test.mjsindex 74fbbe4afc..37d2ed119f 100644--- a/scripts/api-report/api-report.test.mjs+++ b/scripts/api-report/api-report.test.mjs@@ -86,13 +86,28 @@ describe('generateReport', () => {expect(report).toContain('interface ChipExperimentalOptions @experimental');});- it('leaves out ./internal/*, private custom properties, and unlisted attributes', () => {+ it('leaves out ./internal/*, commented-out custom properties, and unlisted attributes', () => {expect(report).not.toContain('secret');- expect(report).not.toContain('--_demo-private');expect(report).not.toContain('--commented-out');expect(report).not.toContain('data-astryx-private');});+ it('leaves out a private --_* custom property', () => {+ expect(report).not.toContain('--_demo-private');+ });++ it('leaves out a custom property name that StyleX hashed', () => {+ expect(report).not.toContain('--x1ue0wla');+ });++ it('still lists a public --astryx-* custom property', () => {+ expect(report).toContain('var --astryx-demo-offset (./demo.css)');+ });++ it('still lists a short authored name in the StyleX prefix form', () => {+ expect(report).toContain('var --xl (./demo.css)');+ });+it('details a declaration once and points later entry points at it', () => {expect(report.match(/^component Chip\(/gm)).toHaveLength(1);expect(report).toContain('Chip (.)');diff --git a/scripts/api-report/generate.mjs b/scripts/api-report/generate.mjsindex 0a9fe8e686..0959b9950b 100644--- a/scripts/api-report/generate.mjs+++ b/scripts/api-report/generate.mjs@@ -25,6 +25,7 @@ import * as fs from 'node:fs';import {createRequire} from 'node:module';import * as path from 'node:path';import {fileURLToPath, pathToFileURL} from 'node:url';+import {STYLEX_GENERATED_VARIABLE} from '../lib/stylex-generated-variable.mjs';const require = createRequire(import.meta.url);const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));@@ -68,6 +69,17 @@ const cap = s =>const kebabAttr = p =>`data-${p.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()}`;+const STYLEX_HASHED_NAME = new RegExp(`^${STYLEX_GENERATED_VARIABLE}$`);++/**+ * Whether a stylesheet's custom property is public: not a private `--_*`+ * name and not a name StyleX hashed.+ * @param {string} name+ * @returns {boolean}+ */+const isPublicCustomProperty = name =>+ !name.startsWith('--_') && !STYLEX_HASHED_NAME.test(name);+/*** The file an entry point publishes: its stylesheet for a stylesheet entry,* otherwise its built declarations, falling back to the module itself.@@ -535,8 +547,8 @@ export async function generateReport(opts) {for (const e of entries.filter(e => e.kind === 'css')) {const css = fs.readFileSync(e.abs, 'utf8').replace(/\/\*[\s\S]*?\*\//g, '');const names = new Set();- for (const m of css.matchAll(/(?:^|[;{\s])(--[A-Za-z][\w-]*)\s*:/g))- names.add(m[1]);+ for (const m of css.matchAll(/(?:^|[;{\s])(--[\w-]+)\s*:/g))+ if (isPublicCustomProperty(m[1])) names.add(m[1]);for (const n of [...names].sort(byName))cssLines.push(`var ${n} (${e.subpath})`);}diff --git a/scripts/build-css.mjs b/scripts/build-css.mjsindex 6ce7d25b5b..99369aa37b 100644--- a/scripts/build-css.mjs+++ b/scripts/build-css.mjs@@ -25,6 +25,7 @@import {transformAsync} from '@babel/core';import stylexBabelPlugin from '@stylexjs/babel-plugin';import stylexResolution from './stylex-module-resolution.cjs';+import {STYLEX_GENERATED_VARIABLE} from './lib/stylex-generated-variable.mjs';import fs from 'node:fs/promises';import path from 'node:path';import {fileURLToPath} from 'node:url';@@ -377,7 +378,7 @@ export function composePackageStylesheet(banner, css) {* object or string, or a `defineConsts` member passed to `firstThatWorks` on* its own. The dynamic-style variables StyleX sets inline (`--x-<property>`)* are not generated names and are never reported, nor is a name that is not- * hash-shaped (StyleX's hash is four to seven base-36 characters).+ * hash-shaped (see STYLEX_GENERATED_VARIABLE).** SYNC: packages/build/src/undefinedVariables.ts is the same check for an* app's production build.@@ -389,9 +390,14 @@ export function undefinedStyleXVariables(css, providerCss = '') {),);const read = new Set(- [...css.matchAll(/var\(\s*(--x[a-z0-9]{4,7})\s*[,)]/g)].map(- match => match[1],- ),+ [+ ...css.matchAll(+ new RegExp(+ String.raw`var\(\s*(${STYLEX_GENERATED_VARIABLE})\s*[,)]`,+ 'g',+ ),+ ),+ ].map(match => match[1]),);return [...read].filter(name => !declared.has(name)).sort();}diff --git a/scripts/lib/stylex-generated-variable.mjs b/scripts/lib/stylex-generated-variable.mjsnew file mode 100644index 0000000000..0df7bf6a91--- /dev/null+++ b/scripts/lib/stylex-generated-variable.mjs@@ -0,0 +1,23 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file The shape of a custom property name that StyleX generates.+ * @input nothing+ * @output STYLEX_GENERATED_VARIABLE, a regular expression source+ * @position scripts/lib — read by scripts/build-css.mjs (undefined generated+ * variables) and scripts/api-report/generate.mjs (custom properties+ * left out of the report)+ *+ * SYNC: packages/build/src/undefinedVariables.ts uses the same hash bound for+ * an app's production build.+ */++/**+ * StyleX names a variable it generates `--x<hash>`: the package builds' class+ * name prefix `x`, then a 32-bit hash in base 36. Only four to seven hash+ * characters count. A hash is shorter than four characters about once in a+ * hundred thousand, and authored short names such as `--xl` keep that form.+ * A dash after the prefix (`--x-<property>`, a dynamic-style variable StyleX+ * sets inline) is not a generated name.+ */+export const STYLEX_GENERATED_VARIABLE = '--x[a-z0-9]{4,7}';