Skip to content

feat: add support for makeResetStyles transforms to CSS extraction#250

Merged
layershifter merged 3 commits intomicrosoft:mainfrom
layershifter:feat/mkrs-css-extraction
Oct 12, 2022
Merged

feat: add support for makeResetStyles transforms to CSS extraction#250
layershifter merged 3 commits intomicrosoft:mainfrom
layershifter:feat/mkrs-css-extraction

Conversation

@layershifter
Copy link
Member

@layershifter layershifter commented Oct 6, 2022

Fixes #231.

  • Adds __resetCSS functions to @griffel/core & @griffel/react, similar to chore: add __resetStyles functions to core & react #249, but these functions don't do insertion to DOM
  • Updates Babel plugin in @griffel/webpack-extraction-plugin to handle __resetStyles
  • Updates sortRules to handle new classes

@github-actions
Copy link

github-actions bot commented Oct 6, 2022

📊 Bundle size report

Unchanged fixtures
Package & Exports Size (minified/GZIP)
core
makeResetStyles (runtime)
16.192 kB
6.041 kB
core
makeStyles + mergeClasses (build time)
1.842 kB
877 B
core
makeStyles + mergeClasses (runtime)
20.789 kB
7.724 kB
react
__css + mergeClasses (build time)
1.879 kB
874 B
react
__styles + mergeClasses (build time)
3.581 kB
1.614 kB
react
makeResetStyles (runtime)
17.957 kB
6.777 kB
react
makeStaticStyles (runtime)
9.443 kB
4.068 kB
react
makeStyles + mergeClasses (runtime)
22.554 kB
8.436 kB
🤖 This report was generated against 9db92f348c4ea7ee453ae4d47499b6fd3bdc652d

@layershifter layershifter force-pushed the feat/mkrs-css-extraction branch from 3c471d6 to f5260f6 Compare October 6, 2022 13:50
Comment on lines +59 to +61
if (isResetClassName(element.value)) {
return 'r';
}
Copy link
Member Author

@layershifter layershifter Oct 6, 2022

Choose a reason for hiding this comment

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

This does not seem to scale well. We will need probably to revisit this place with a more solid options. But before we will need to confirm that there are real troubles already...

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry I don't understand why this is not scalable

Copy link
Member Author

@layershifter layershifter Oct 12, 2022

Choose a reason for hiding this comment

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

It's not handling global selectors and few other cases:

body .baz { color: orange; }
/* ⬇️⬇️⬇️ */
/* element.value is "body .baz" */

I will work on integration of CSS extraction to one of products and will be looking into potential issues to see how to make sorting better (or reimplement it completely).

P.S. It worth creating an issue, but as I don't all cases yet I postponed creating it.

@layershifter layershifter marked this pull request as ready for review October 6, 2022 14:03
@layershifter layershifter requested a review from a team as a code owner October 6, 2022 14:03
import { COMMENT, compile, Element, KEYFRAMES, MEDIA, RULESET, serialize, stringify, SUPPORTS, tokenize } from 'stylis';

function isResetClassName(className: string) {
return className[0] === '.' && className[1] === RESET_HASH_PREFIX;
Copy link
Contributor

Choose a reason for hiding this comment

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

why do you need to check that the first character is . ?

Copy link
Contributor

Choose a reason for hiding this comment

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

can stylis return something other than . as the first character ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, tr .foo for example.

const cssRules = evaluationResult.value as string[];

state.cssRules!.push(...cssRules);
argumentPaths[2].remove();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
argumentPaths[2].remove();
argumentPaths.remove();

you set the argumentPath correctly for both cases

Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest moving argumentPath.remove() out of conditions. Likewise you can also just initialize cssRules as an empty array earlier on and move the state update out of conditions too

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 1aeddeb

}

if (argumentPaths.length !== 2) {
if (functionKind === '__styles' && argumentPaths.length !== 2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you could just parametrize the two errors ?

Copy link
Contributor

Choose a reason for hiding this comment

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

you could actually parametrize a lot:

  • argument position
  • argument length
  • replacement function kind

It avoids some duplication, but it's not a lot so it's your call

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 99a99ac

@layershifter layershifter merged commit 6350963 into microsoft:main Oct 12, 2022
@layershifter layershifter deleted the feat/mkrs-css-extraction branch October 12, 2022 15:38
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.

css-extraction: handle makeResetStyles

2 participants