Skip to content
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

[react] Fix scoping issue with sx dynamic value transform #286

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

brijeshb42
Copy link
Contributor

@brijeshb42 brijeshb42 commented Oct 24, 2024

For this sample component -

function App(props) {
  return (
    <SliderRail
      {...props}
      sx={[
        props.variant === 'secondary'
          ? { color: props.isRed ? 'red' : 'blue' }
          : { backgroundColor: 'blue', color: 'white' },
      ]}
    />
  );
}

In the current sx transform, we were transforming the dynamic part to an arrow function with the same arguments that it was using internally and then later in the sx.ts based wyw transform, we were just replacing the arrow function with just its body (to be same as user code). This was being done to play well with wyw were it throws an error if it tries to evaluate an object that references local variables.

Current transform -

sx={[
  props.variant === 'secondary'
    ? { color: (props) => props.isRed ? 'red' : 'blue' }
    : { backgroundColor: 'blue', color: 'white' },
]}

This did not play well with minifiers where the variables in transformed function would be renamed like -

 (_props) => _props.isRed ? 'red' : 'blue'

So later when we were trying to revert back to user code, it'll just be

{ color: _props.isRed ? 'red' : 'blue' }

resulting in an issue trying to use a variable that does not exist. This was more apparent when using webpack with it's terser based uglifier.

This PR fixes the issue by serializing the AST as a string and later on deserializing it and generating the code from the AST.

Partially fixes #43597

Sorry, something went wrong.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

Looks good. Is it possible to add a test case for this?

@brijeshb42
Copy link
Contributor Author

Not specifically for this. The motivation here was also that existing tests do not break regardless of the internal changes.
So in a way, we already had tests.
Actual test might require an elaborate setup with terser of some other uglifier.

@siriwatknp
Copy link
Member

Not specifically for this. The motivation here was also that existing tests do not break regardless of the internal changes. So in a way, we already had tests. Actual test might require an elaborate setup with terser of some other uglifier.

I see.

Comment on lines 104 to 109
// eslint-disable-next-line no-nested-ternary
const cssKey = key.isIdentifier()
? key.node.name
: key.isStringLiteral()
? key.node.value
: '';
Copy link
Member

Choose a reason for hiding this comment

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

better to remove ternary or remove this lint config.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 one nit.

@brijeshb42 brijeshb42 merged commit c925f36 into mui:master Oct 24, 2024
12 checks passed
@brijeshb42 brijeshb42 deleted the scoping-fix branch October 24, 2024 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrating to Pigment CSS with react/webpack
3 participants