8000 [babel-plugin] add LTR/RTL config to legacy-e… by mellyeliu · Pull Request #1083 · facebook/stylex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[babel-plugin] add LTR/RTL config to legacy-e… #1083

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 3 commits into from
May 28, 2025
Merged

Conversation

mellyeliu
Copy link
Member
@mellyeliu mellyeliu commented May 28, 2025

Context

Let's remove the LTR/RTL polyfill (#752), as most browsers support logical styles now:

legacy-expand-shorthands works in a few steps:

  1. In legacy-expand-shorthands.js: We split shorthands into respective longhands. There are no (common) shorthands left after this.
  2. In /physical-rtl we convert these into ltr: marginLeft and rtl: marginRight.

Follow ups:

  • In 1., we do some transforms (marginInlineStart -> to RN equivalent marginStart, for legacy internal reasons) that we need to remove, since this would be exposed without being converted to marginLeft and marginRight
    • Let's do this in this PR so we don't have any state where we're shipping margin-start css properties
  • We should consider adding an exception to border-*-*-radius properties, though it's on the edge of browser support. They fit the internal browser requirements for every app except WhatsApp.

Testing

image

For these tests, we expect to see the LTR/RTL polyfill when enableLegacyDirectionPolyfill: true. We expect to the unmodified logical styles when enableLegacyDirectionPolyfill: false.

Note: We want to migrate over to application-order (and later property-specificity), but it'll be a long process to test and ship @layer. In the meantime, let's remove the LTR/RTL polyfill as a way to test experimental stylesheets. We use the legacy-expand-shorthands style resolution over application-order or property-specificity internally for this reason:

  • legacy-expand-shorthands gets rid of the most common shorthands and expands them, since without @layer use in processStylexRules, we run into ordering issues with shorthands

@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 May 28, 2025
Copy link
github-actions bot commented May 28, 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.kVI9LXjjTK /tmp/tmp.Tm1rZykl7C

Results Base Patch Ratio
babel-plugin: stylex.create
· basic create 542 532 0.98 -
· complex create 193 191 0.99 -
babel-plugin: stylex.createTheme
· basic themes 461 452 0.98 -
· complex themes 42 42 1.00

Copy link
github-actions bot commented May 28, 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.62K4x8p9wg /tmp/tmp.t9OETnFgaA

Results Base Patch Ratio
@stylexjs/stylex/lib/cjs/stylex.js
· compressed 1,222 1,200 0.98 -
· minified 3,679 3,513 0.95 -
@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

Copy link
Contributor
@necolas necolas left a comment

Choose a reason for hiding this comment

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

The mapping to marginStart etc is not really for RN, it's just the legacy non-standard names we used internally before moving into the standard ones.

We could also move the related polyfill tests into the 'legacy' directory

@@ -53,19 +53,19 @@ describe('Legacy-shorthand-expansion resolution', () => {
var _inject2 = _inject;
import stylex from 'stylex';
_inject2(".x123j3cw{padding-top:5px}", 4000);
_inject2(".x1mpkggp{padding-right:5px}", 3000, ".x1mpkggp{padding-left:5px}");
_inject2(".x1gabggj{padding-right:5px}", 3000, ".x1gabggj{padding-left:5px}");
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm can we avoid having these hashes change?

Copy link
Member Author

Choose a reason for hiding this comment

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

i'm actually not sure why these are changing and have to investigate...

Copy link
Member Author

Choose a reason for hiding this comment

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

just dug into this a bit. in the stylex-create visitor, we build the hash using the property name after shorthand expansion (padding -> padding-top, padding-start), but before ltr/rtl polyfill (see below).

the hashes changed because i made a change to expand to padding-inline-start instead of padding-start, but the polyfill later changes it back to paddingLeft and paddingRight

Comment on lines +729 to +730
_inject2(".x1hm9lzh{margin-left:10px}", 3000, ".x1hm9lzh{margin-right:10px}");
_inject2(".x1sa5p1d{margin-right:10px}", 3000, ".x1sa5p1d{margin-left:10px}");
Copy link
Contributor
@necolas necolas May 28, 2025

Choose a reason for hiding this comment

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

Is it expected that the classname hash is the same for 2 different rules? Is this just how we've always done it?

8000

Copy link
Member Author

Choose a reason for hiding this comment

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

also dug into this, I think it's because:

<div class = "x1hm9lzh ..." />

we serve two different stylesheets depending on if it's LTR/RTL, but want to avoid adding two classnames, so in LTR it gets the .x1hm9lzh {margin-left } rule and in RTL it gets the .x1hm9lzh {margin-right}

@mellyeliu mellyeliu changed the title [babel-plugin] add enableLegacyDirectionPolyfill config to legacy-e… [babel-plugin] add LTR/RTL config to legacy-e… May 28, 2025
const propertyToLTR: $ReadOnly<{
[key: string]: ($ReadOnly<[string, string]>) => $ReadOnly<[string, string]>,
}> = {
'margin-start': ([_key, val]: $ReadOnly<[string, string]>) => [
'margin-left',
val,
],
// 'margin-inline-start': ([_key, val]) => ['margin-left', val],
Copy link
Member Author
@mellyeliu mellyeliu May 28, 2025

Choose a reason for hiding this comment

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

these are previous "aliases" that didn't need to be explicitly included, because we converted instances of marginInlineStart to marginStart while expanding shorthands. since we're expanding to marginInlineStart now, we need to include this in the mapping.

now included in inlinePropertyToLTR and used when LTR/RTL fallback is on and style resolution is legacy-expand-shorthands. after we ship this with experimental stylesheets we can try to remove most of this logic altogether

['marginBottom', bottom],
['marginStart', left],
Copy link
Member Author

Choose a reason for hiding this comment

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

this is where we previously expanded margin to marginStart anyway, since it all got translated to marginLeft/marginRight downstream

@mellyeliu mellyeliu marked this pull request as ready for review May 28, 2025 20:05
@@ -53,19 +53,19 @@ describe('Legacy-shorthand-expansion resolution', () => {
var _inject2 = _inject;
import stylex from 'stylex';
_inject2(".x123j3cw{padding-top:5px}", 4000);
_inject2(".x1mpkggp{padding-right:5px}", 3000, ".x1mpkggp{padding-left:5px}");
_inject2(".x1gabggj{padding-inline-end:5px}", 3000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should turn the feature flag on for these tests, if they're basically to test that the way we current use StyleX internally is not changed?

@mellyeliu mellyeliu merged commit afcbc82 into main May 28, 2025
9 checks passed
@mellyeliu mellyeliu deleted the ltr-expansion branch May 28, 2025 20:42
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