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

Compress atomic class names #1408

Merged
merged 39 commits into from Feb 20, 2023
Merged

Compress atomic class names #1408

merged 39 commits into from Feb 20, 2023

Conversation

liamqma
Copy link
Collaborator

@liamqma liamqma commented Feb 6, 2023

This PR does

  • updates ax function to accept short class name
  • updates babel-plugin to compress class names based on pre-bundling hash map
  • generates compression map

@changeset-bot
Copy link

changeset-bot bot commented Feb 6, 2023

🦋 Changeset detected

Latest commit: 62aab19

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

This PR includes changesets to release 11 packages
Name Type
@compiled/parcel-transformer Minor
@compiled/webpack-loader Minor
@compiled/babel-plugin Minor
@compiled/react Minor
@compiled/css Minor
@compiled/parcel-config Patch
@compiled/parcel-optimizer-test-app Patch
@compiled/parcel-transformer-test-app Patch
@compiled/parcel-transformer-test-custom-resolver-app Patch
@compiled/parcel-transformer-test-extract-app Patch
@compiled/parcel-optimizer 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
Copy link
Collaborator Author

liamqma commented Feb 6, 2023

TODO

  • Look into the possibility of class name clash with other CS libraries (styled-components and emotion)
  • Changeset
  • Parcel integration
  • Turn off the feature if stylesheet extraction is off

@liamqma liamqma requested a review from dddlr February 6, 2023 22:58
pancaspe87

This comment was marked as duplicate.

'_aaaacccc',
],
[
'should ensure the last atomic declaration of a single group with short class name wins',
Copy link
Collaborator

Choose a reason for hiding this comment

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

I haven't followed up the previous PRs and I am still wrapping my head around these changes. Hence, apologies if this was mentioned somewhere else

I am assuming that:

  • _aaaabbbb is the CSS class with no manipulation
  • a is the optimised class, which is stored in the map
  • _aaaa_a is made of atomic group + optimised class -> in this way we are able to handle overrides

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, spot on!

@@ -0,0 +1,27 @@
{
"1wyb12am": "a",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where/how does this map get generated?

Copy link
Collaborator Author

@liamqma liamqma Feb 8, 2023

Choose a reason for hiding this comment

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

Good question! For now it's assumed that library consumers manually curate the map and pass it to the babel-plugin. We will later create an automatic method to generate the map by statically analysing the codebase, and the map should be periodically updated by a build.

@@ -201,4 +201,166 @@ describe('babel plugin', () => {

expect(actual).toInclude('c_MyDiv');
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still have cases where compression is not safe, hence we should bail out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Compression should be safe everywhere now.

@liamqma liamqma marked this pull request as draft February 9, 2023 00:07
builtCss.css.map((x) => getItemCss(x)).join(''),
meta.state.opts
);
const { sheets, classNames } = transformCssItems(builtCss.css, meta);
Copy link
Collaborator Author

@liamqma liamqma Feb 9, 2023

Choose a reason for hiding this comment

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

The change in this file is to ensure <ClassNames> handles conditional CSS rules.
i.e.

<ClassNames>
          {({ css }) => (<div className={css({ ...props.isPrimary && { color: 'blue' }})}>hello, world!</div>)}
</ClassNames>

const compressedClassName =
classNameCompressionMap && classNameCompressionMap[fullClassName.slice(1)];

if (compressedClassName) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If compressed class name is available, we are replacing full class name with the compressed in the sheet.
So

.aaaabbbb { font-size: 10px }

becomes

.a { font-size: 10px }

It will reduce the size of stylesheet but will break if we don't replace every occurrence of _aaaabbbb, which I should have done in this PR. 🤞

renovate bot and others added 21 commits February 16, 2023 10:05
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
* Bump node version to v18

* Bump import jsx

* Bump nvmrc node version from 18.12 to 18.14

* Add changeset for PR #1392

---------

Co-authored-by: Grant Wong <gwong2@atlassian.com>
* chore(deps): update parcel packages

* Add dummy generic type so optimizer works with parcel v2.8.0+

Parcel v2.8.0 adds a BundleConfigType generic type to Optimizer
(parcel-bundler/parcel#8370) as part of
their `loadBundleConfig` method. We use a dummy type because we
don't need this functionality currently.

* Update snapshot tests for Parcel 2.8.3

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Grant Wong <gwong2@atlassian.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
* chore(deps): update dependency jest to v29

* Update snapshots and node env for some tests

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Jake Lane <jlane2@atlassian.com>
Co-authored-by: Grant Wong <2908767+dddlr@users.noreply.github.com>
@dddlr
Copy link
Collaborator

dddlr commented Feb 16, 2023

Thank you Liam for updating the PR :) This looks good to me, but I'll wait for an approval from @JakeLane or @pancaspe87 too since I don't have a good understanding of the whole codebase

@liamqma liamqma merged commit f9c957e into master Feb 20, 2023
@liamqma liamqma deleted the compress-atomic-class-names branch February 20, 2023 03:01
@liamqma liamqma mentioned this pull request Apr 13, 2023
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.

None yet

6 participants