-
Notifications
You must be signed in to change notification settings - Fork 313
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
feat: dynamic styles set inherits to false #794
Conversation
workflow: benchmarks/sizeComparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.
|
// add injection that mark variables used for dynamic styles as `inherits: false` | ||
const injectedInheritStyles: { [string]: InjectableStyle } = {}; | ||
if (fns != null) { | ||
const dynamicFnsNames = Object.values(fns) | ||
?.map((entry) => Object.keys(entry[1])) | ||
.flat(); | ||
dynamicFnsNames.forEach((fnsName) => { | ||
injectedInheritStyles[fnsName] = { | ||
priority: 0, | ||
ltr: `@property ${fnsName} { inherits: false }`, | ||
rtl: null, | ||
}; | ||
}); | ||
} | ||
|
||
if (!confident) { | ||
throw path.buildCodeFrameError(messages.NON_STATIC_VALUE, SyntaxError); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure you add this code after the if
condition that checks confident
. Otherwise you'll end up throwing errors because fns
will be null
or undefined
.
// add injection that mark variables used for dynamic styles as `inherits: false` | |
const injectedInheritStyles: { [string]: InjectableStyle } = {}; | |
if (fns != null) { | |
const dynamicFnsNames = Object.values(fns) | |
?.map((entry) => Object.keys(entry[1])) | |
.flat(); | |
dynamicFnsNames.forEach((fnsName) => { | |
injectedInheritStyles[fnsName] = { | |
priority: 0, | |
ltr: `@property ${fnsName} { inherits: false }`, | |
rtl: null, | |
}; | |
}); | |
} | |
if (!confident) { | |
throw path.buildCodeFrameError(messages.NON_STATIC_VALUE, SyntaxError); | |
} | |
if (!confident) { | |
throw path.buildCodeFrameError(messages.NON_STATIC_VALUE, SyntaxError); | |
} | |
// add injection that mark variables used for dynamic styles as `inherits: false` | |
const injectedInheritStyles: { [string]: InjectableStyle } = {}; | |
if (fns != null) { | |
const dynamicFnsNames = Object.values(fns) | |
?.map((entry) => Object.keys(entry[1])) | |
.flat(); | |
dynamicFnsNames.forEach((fnsName) => { | |
injectedInheritStyles[fnsName] = { | |
priority: 0, | |
ltr: `@property ${fnsName} { inherits: false }`, | |
rtl: null, | |
}; | |
}); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call, thanks for fixing it!
I noticed that this change didn't show up in the CSS bundle size benchmark. We don't have any dynamic styles syntax used in the fixture data. It would be good to capture the impact on bundle size by updating the benchmark's fixture data to include a significant number of dynamic styles. (The original fixture was scraped from a snapshot of Comet that pre-dates support for dynamic styles.) When using inline styles directly, you don't incur the runtime cost of using custom properties that necessitates this patch. For most dynamic use cases, we can simply render inline styles without using custom properties or adding generated CSS. There are runtime and bundle size wins to be had there. With improved fixture data we should be able to get a sense of how to quantify the impact of future optimizations to the implementation. |
* feat: dynamic styles set inherits to false * update tests
* feat: dynamic styles set inherits to false * update tests
What changed / motivation ?
Addresses task #734.
Generate
@property
declarations for all variables that are created for dynamic styles and declare them as not inheriting. Increase stylex variable compiler efficiency by disabling all dynamic styles from cascading down from the node defining the variable.Linked PR/Issues
Fixes #734
Additional Context
npx jest stylex-transform-create-test
Test file stylex-transform-create-test.js should have
@property
injection added for all stylex dynamic styles.Pre-flight checklist
Contribution Guidelines