8000 [babel-plugin] Improve createTheme and defineVars tests by necolas · Pull Request #1099 · facebook/stylex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[babel-plugin] Improve createTheme and defineVars tests #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025

Conversation

necolas
Copy link
Contributor
@necolas necolas commented Jun 17, 2025

Unit tests to better capture the way that theming currently works in StyleX.

Background:

  • #630 describes how StyleX theming used to work, and that it wasn't matching user expectations.
  • A solution was found to match expectations.
  • 5f14ff58 is a patch that made a change to how StyleX theming works.
  • I recently noticed in the React Strict DOM example app that StyleX-based theming regressed on web.

Patch:

  • Make sure we capture the complete metadata in the createTheme tests. Previously, we did not have visibility into all the CSS rules generated.
  • Add extra tests to capture scenarios where multiple variables and themes are created across files. These tests surface that a different "root theme" class name is created for each different file the contains defineVars calls.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 17, 2025
Copy link
github-actions bot commented Jun 17, 2025

workflow: benchmarks/perf

Comparison of performance test results, measured in operations per second. Larger is better.

benchmarks@0.13.1 compare
node ./compare.js /tmp/tmp.DpvUaxQs68 /tmp/tmp.qCfkur81BO

Results Base Patch Ratio
babel-plugin: stylex.create
· basic create 540 528 0.98 -
· complex create 198 193 0.97 -
babel-plugin: stylex.createTheme
· basic themes 462 460 1.00 -
· complex themes 43 42 0.98 -

Copy link
github-actions bot commented Jun 17, 2025

workflow: benchmarks/size

Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.

benchmarks@0.13.1 compare
node ./compare.js /tmp/tmp.3NEAP4U5cS /tmp/tmp.kwkeq2C14T

Results Base Patch Ratio
@stylexjs/stylex/lib/cjs/stylex.js
· compressed 1,222 1,222 1.00
· minified 3,679 3,679 1.00
@stylexjs/stylex/lib/cjs/inject.js
· compressed 1,227 1,227 1.00
· minified 3,224 3,224 1.00
benchmarks/size/.build/bundle.js
· compressed 537,611 537,611 1.00
· minified 7,435,904 7,435,904 1.00
benchmarks/size/.build/stylex.css
· compressed 100,509 100,509 1.00
· minified 754,513 754,513 1.00

Comment on lines +76 to +86
const { code, metadata } = transform(
`
${_code}
${fixture}
`;
`,
pluginOptions,
);

_metadata.stylex.push(...metadata.stylex);

return { code, metadata: _metadata };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test abstraction exists because we're trying to test the integration between defineVars and createTheme. But, we were only taking snapshots of partial metadata, missing the "default theme" CSS rules. To fix that, I chose to refactor this helper

Comment on lines +125 to +148
[
"xop34xu",
{
"ltr": ":root, .xop34xu{--xwx8imx:blue;--xaaua2w:grey;--xbbre8:10;}",
"rtl": null,
},
0.1,
],
[
"xop34xu-1lveb7",
{
"ltr": "@media (prefers-color-scheme: dark){:root, .xop34xu{--xwx8imx:lightblue;--xaaua2w:rgba(0, 0, 0, 0.8);}}",
"rtl": null,
},
0.1,
],
[
"xop34xu-bdddrq",
{
"ltr": "@media print{:root, .xop34xu{--xwx8imx:white;}}",
"rtl": null,
},
0.1,
],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of "default theme" CSS that now shows up in the snapshots

@@ -167,6 +203,30 @@ describe('@stylexjs/babel-plugin', () => {
expect(metadata).toMatchInlineSnapshot(`
{
"stylex": [
[
"xop34xu",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This specific hash / class name - xop34xu - appears in most of these snapshots because we frequently use /stylex/packages/vars.stylex.js as the default file name

export const theme = stylex.createTheme(vars, ${themeObject});
`);
const { code, metadata } = transform(fixture, options);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options here were meant to be passed to the previous transform call (inside createFixture)

{
"ltr": ".x4aw18j, .x4aw18j:root{--xwx8imx:green;--xaaua2w:antiquewhite;--xbbre8:6px;}",
"ltr": ":root, .x1xohuxq{--xt4ziaz:blue;--x1e3it8h:grey;--x1onrunl:10;}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We fixed this test to use the vars filename correctly. Now we see that the "default theme" class name hash has changed from xop34xu to x1xohuxq.

* Make sure we capture the complete metadata in the createTheme tests.
* Add extra tests to capture scenarios where multiple variables and
  themes are created across files.
@necolas necolas merged commit d6c0243 into main Jun 17, 2025
9 checks passed
@necolas necolas deleted the babel-plugin/fix-theming-tests branch June 17, 2025 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0