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

CSS Map alternative compilation approach #1496

Merged
merged 18 commits into from
Aug 29, 2023
Merged

CSS Map alternative compilation approach #1496

merged 18 commits into from
Aug 29, 2023

Conversation

liamqma
Copy link
Collaborator

@liamqma liamqma commented Aug 23, 2023

This PR presents a different compilation approach compared to #1492.

It transforms

const styles = cssMap({
  danger: {
      color: 'red',
      backgroundColor: 'red'
  },
  success: {
    color: 'green',
    backgroundColor: 'green'
  }
});
<div css={css([styles[variant]) />

to

const styles = {
  danger: "_syaz5scu _bfhk5scu",
  success: "_syazbf54 _bfhkbf54",
};
<div className={ax([styles[variant]) />

The benefits are:

  • dramatically simplify evaluation as we no longer evaluate styles[variant].
  • paves the way for transforming css the same way as cssMap.
    In the future, we can transform
const foo = css({ 
    color: 'red' 
})

const bar = cssMap({
    danger: {
       color: 'red'
    },
    success: {
      color: 'green'
    }
});

<div css={css([foo, bar[variant]])} />

to

const foo = ".a";

const bar = {
    danger:".a",
    success: ".b",
}
<div className={ax([foo, bar[variant]])} />

@changeset-bot
Copy link

changeset-bot bot commented Aug 23, 2023

🦋 Changeset detected

Latest commit: 717b429

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@compiled/babel-plugin Minor
@compiled/webpack-app Minor
@compiled/parcel-app Minor
@compiled/react Minor
@compiled/parcel-transformer Patch
@compiled/webpack-loader Patch
@compiled/parcel-optimizer-test-app Patch
@compiled/parcel-transformer-test-app Patch
@compiled/parcel-transformer-test-compress-class-name-app Patch
@compiled/parcel-transformer-test-custom-resolver-app Patch
@compiled/parcel-transformer-test-extract-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@liamqma liamqma force-pushed the css-map-2 branch 2 times, most recently from ef4bcce to 4738f0d Compare August 24, 2023 23:13
foo && styles["danger"],
props.foo && styles["danger"],
styles.success,
styles["danger"],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it worth statically embedding this? We can reduce the amount of runtime code by directly injecting the classnames we know here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea 👍

Copy link
Collaborator Author

@liamqma liamqma Aug 25, 2023

Choose a reason for hiding this comment

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

I want to minimise statical evaluation (which is 🐰🕳️). I am going to leave this job to the bundler.

I just tested a JFE Webpack/Parcel prod build. It inlines the variable well.

const foo = { bar: '.aaaabbbb' }
<div className={foo.bar} />

to

<div className={'.aaaabbbb'} />

@JakeLane
Copy link
Collaborator

Can you please add some unit tests for correct usage of the API and some VR tests?

Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens if the key is not in the css map?

Copy link
Collaborator Author

@liamqma liamqma Aug 25, 2023

Choose a reason for hiding this comment

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

then class name will be undefined and stripped out by ax. The idea is to reply on type safety to detect problems, instead of build-time errors.

Copy link
Collaborator

Choose a reason for hiding this comment

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

makes sense, I assume that should help reduce the impact on build time

@liamqma
Copy link
Collaborator Author

liamqma commented Aug 27, 2023

Can you please add some unit tests for correct usage of the API and some VR tests?

Sure!

Copy link
Collaborator

@JakeLane JakeLane left a comment

Choose a reason for hiding this comment

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

Nice work!

@liamqma liamqma merged commit 4a2174c into master Aug 29, 2023
5 checks passed
@liamqma liamqma deleted the css-map-2 branch August 29, 2023 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants