Skip to content

Conversation

@mellyeliu
Copy link
Member

@mellyeliu mellyeliu commented Dec 1, 2025

Currently, we output all defineVars and createTheme variables in the compiled sheet even if such variables are never used. This is costly for design systems that define large token objects via defineVars, because the browser ends up allocating memory for thousands of unused variables.

This PR optimizes two cases:

  1. Removes all defineVars generated variables if they are never referenced
  2. Inlines defineVars CSS variables with their values if CSS variables are used once

Important: This is behind a param optimizeDefineVars because this cannot yet be used internally. We don't have global variable knowledge and would need to modify our build step to bundle all defineVars files with each sheet. optimizeDefineVars must be set to false where we pipe out to processStylexRules on next sync

Todo:

  • Improve tests
  • Refactor and clean up regex patterns

@meta-cla meta-cla 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 Dec 1, 2025
@supports (color: oklab(0 0 0)){@media (prefers-color-scheme: dark){:root, .xsg933n{--colorTokens-xkxfyv:oklab(0.7 -0.3 -0.4);}}}"
`);
).toMatchInlineSnapshot(
'"@supports (color: oklab(0 0 0)){@media (prefers-color-scheme: dark){:root, .xsg933n{--colorTokens-xkxfyv:oklab(0.7 -0.3 -0.4);}}}"',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is likely not valid. This transform ONLY transforms the main file and NO styles are used. So the expected result is an empty string.

Comment on lines +627 to +629
.x1s2izit{padding:8px}
.x1bg2uv5{border-color:green}
@media (max-width: 1000px){.xio2edn.xio2edn{border-color:var(--xpqh4lw)}}
@media (max-width: 1000px){.xio2edn.xio2edn{border-color:blue}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is also invalid because it is inlining values of variables. We don't want to do this in most cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's put this behind a separate config

@nmn
Copy link
Collaborator

nmn commented Dec 11, 2025

  1. Inlines defineVars CSS variables with their values if CSS variables are used once

This needs to be a separate option and not enabled as the same thing.

Also, we need to special-case variable names that start with -- as that is an indication that the variables may be used outside of StyleX.

Comment on lines +396 to +397
const defineVarsVariables: Map<string, string> = new Map();
const varUsageCount: Map<string, number> = new Map();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer using objects for this. They are faster when the key is a string.


for (const [, ruleObj] of rules) {
const { ltr, rtl } = ruleObj;
const isDefineVarsRule = /^(?:@[^{]*\{)?:root,\s*\.[\w-]+\{/.test(ltr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably want to change the metadata generated by defineVars before this PR and avoid regex-based fragile implementations.

@mellyeliu
Copy link
Member Author

Should've marked this as a draft. Just put it up as a placeholder

@mellyeliu mellyeliu marked this pull request as draft December 12, 2025 03:59
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.

Not tree shaking styles during dev mode Post-process generated CSS to remove unused variables and inline constants

3 participants