-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I have these changes sitting locally for a very long time, and it's time to offload them. The idea behind the styles rehydration is very simple: we collect all existing styles from `<style data-adeira-sx />` (once) and use this information later for deciding whether the styles should be injected or not (this is what I call rehydration). The difference from the current solution is that we do it only once and we don't have to go through all the styles everytime there is something to inject. Motivation for this change is performance: this should significantly improve the runtime style perf + be much easier to deal with (for example, we are basically brute-forcing @at rules and pseudo rules now). Note: this is just a partial solution, there are other changes coming (custom styles printer and @at nodes/pseudo nodes optimization). Related issue: #1579
- Loading branch information
Showing
16 changed files
with
493 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// @flow | ||
|
||
import printNodes from '../printNodes'; | ||
import StyleCollectorAtNode from '../StyleCollectorAtNode'; | ||
import StyleCollectorNode from '../StyleCollectorNode'; | ||
import StyleCollectorPseudoNode from '../StyleCollectorPseudoNode'; | ||
|
||
it('prints `StyleCollectorNode` as expected', () => { | ||
expect( | ||
printNodes([ | ||
new StyleCollectorNode('color', 'red'), | ||
new StyleCollectorNode('color', 'green'), | ||
new StyleCollectorNode('color', 'blue'), | ||
]), | ||
).toMatchInlineSnapshot(` | ||
"._324Crd{color:#f00} | ||
.mRoJ3{color:#008000} | ||
._2dHaKY{color:#00f}" | ||
`); | ||
}); | ||
|
||
it('prints `StyleCollectorPseudoNode` as expected', () => { | ||
expect( | ||
printNodes([ | ||
new StyleCollectorPseudoNode( | ||
':hover', | ||
new Map([ | ||
// TODO: are the map keys necessary (?) | ||
['c0', new StyleCollectorNode('color', 'red')], | ||
['c1', new StyleCollectorNode('color', 'green')], | ||
['c2', new StyleCollectorNode('color', 'blue')], | ||
]), | ||
), | ||
]), | ||
).toMatchInlineSnapshot(` | ||
"._324Crd:hover{color:#f00} | ||
.mRoJ3:hover{color:#008000} | ||
._2dHaKY:hover{color:#00f}" | ||
`); | ||
}); | ||
|
||
it('prints `StyleCollectorAtNode` as expected', () => { | ||
expect( | ||
printNodes([ | ||
new StyleCollectorAtNode( | ||
'@media screen', | ||
new Map([ | ||
['c0', new StyleCollectorNode('fontSize', '14')], | ||
[ | ||
'c1', | ||
new StyleCollectorPseudoNode( | ||
':hover', | ||
new Map([['c0', new StyleCollectorNode('color', 'pink')]]), | ||
), | ||
], | ||
|
||
[ | ||
'c2', | ||
new StyleCollectorAtNode( | ||
'@media (max-width: 12cm)', | ||
new Map([['c0', new StyleCollectorNode('color', 'blue')]]), | ||
), | ||
], | ||
]), | ||
), | ||
]), | ||
).toMatchInlineSnapshot(` | ||
"@media screen{ | ||
._1fVgat._1fVgat{font-size:14} | ||
._3ncx7d._3ncx7d:hover{color:#ffc0cb} | ||
@media (max-width: 12cm){ | ||
._2dHaKY._2dHaKY{color:#00f} | ||
} | ||
}" | ||
`); | ||
}); | ||
|
||
// TODO: prefixing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @flow | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/* global document */ | ||
|
||
import { invariant } from '@adeira/js'; | ||
|
||
import rehydrateStyles from '../rehydrateStyles'; | ||
|
||
it('correctly rehydrates styles from a style element', () => { | ||
// First, we need to create a `CSSStyleSheet` by actually creating a style element: | ||
const styleElement = document.createElement('style'); | ||
document.head?.appendChild(styleElement); | ||
const styleSheet = styleElement.sheet; | ||
|
||
invariant(styleSheet != null, 'Unable to create test StyleSheet.'); | ||
|
||
// Insert some simple CSS rules: | ||
styleSheet.insertRule('._2tPCgL { font-size: 10px; }', 0); | ||
styleSheet.insertRule('._1Kmfck:hover { color: rgba(var(--sx-foreground), 0.5); }', 1); | ||
|
||
// Insert some @at rules: | ||
styleSheet.insertRule( | ||
`@media (prefers-reduced-motion: reduce) { .VdrO3.VdrO3 { animation-duration: 1s; } }`, | ||
2, | ||
); | ||
styleSheet.insertRule( | ||
`@media (prefers-reduced-motion: reduce) { ._2tPCgL._2tPCgL { font-size: 10px; } }`, | ||
3, | ||
); | ||
styleSheet.insertRule( | ||
`@keyframes oxCh9 { 33% { transform: translateY(-10px); } 66% { transform: translateY(10px); } } | ||
`, | ||
4, | ||
); | ||
|
||
// We should be able to decide whether the style needs to be injected later based on the | ||
// following information: | ||
expect(rehydrateStyles(styleSheet)).toMatchInlineSnapshot(` | ||
Object { | ||
"rehydratedKeyframeRules": Set { | ||
"oxCh9", | ||
}, | ||
"rehydratedMediaRules": Map { | ||
"(prefers-reduced-motion: reduce)" => Set { | ||
".VdrO3.VdrO3", | ||
"._2tPCgL._2tPCgL", | ||
}, | ||
}, | ||
"rehydratedStyleRules": Set { | ||
"._2tPCgL", | ||
"._1Kmfck:hover", | ||
}, | ||
} | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.