-
Notifications
You must be signed in to change notification settings - Fork 384
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
Add tree shaking for attribute-extant selectors #2080
Conversation
|
…th other extractions
8f224a3
to
7e84717
Compare
(Comment moved to description) |
Twenty NineteenAs expected/desired, the JS-polyfilled 309,320d308
< .wp-block-quote[style*="text-align:right"], .wp-block-quote[style*="text-align: right"] {
< border-left: none;
< border-right: 4px solid #000;
< padding-left: 0;
< padding-right: 1em
< }
<
< .wp-block-quote[style*="text-align:center"], .wp-block-quote[style*="text-align: center"] {
< border: none;
< padding-left: 0
< }
<
959,967d946
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu {
< display: block;
< left: 0;
< margin-top: 0;
< opacity: 1;
< width: auto;
< min-width: 100%
< }
<
978,993d956
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu {
< display: block;
< margin-top: 0;
< opacity: 1;
< position: absolute;
< left: 0;
< right: auto;
< top: auto;
< bottom: auto;
< height: auto;
< min-width: -moz-max-content;
< min-width: -webkit-max-content;
< min-width: max-content;
< transform: none
< }
<
1011,1017d973
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu.hidden-links {
< left: 0;
< width: 100%;
< display: table;
< position: absolute
< }
<
1026,1032d981
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu.hidden-links {
< right: 0;
< left: auto;
< display: block;
< width: max-content
< }
<
1041,1044d989
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu .submenu-expand {
< display: none
< }
<
1049,1057d993
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu .sub-menu {
< display: block;
< margin-top: inherit;
< position: relative;
< width: 100%;
< left: 0;
< opacity: 1
< }
<
1068,1072d1003
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu .sub-menu {
< float: none;
< max-width: 100%
< }
<
1079,1082d1009
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu .sub-menu {
< counter-reset: submenu
< }
<
1087,1093d1013
< .main-navigation .main-menu .menu-item-has-children:not(.off-canvas)[focus-within] > .sub-menu .sub-menu > li > a::before {
< font-family: "NonBreakingSpaceOverride", "Hoefler Text", "Baskerville Old Face", Garamond, "Times New Roman", serif;
< font-weight: normal;
< content: "– " counters(submenu, "– ", none);
< counter-increment: submenu
< }
< Twenty SixteenNote the 309,320d308
< .wp-block-quote[style*="text-align:right"], .wp-block-quote[style*="text-align: right"] {
< border-left: none;
< border-right: 4px solid #000;
< padding-left: 0;
< padding-right: 1em
< }
<
< .wp-block-quote[style*="text-align:center"], .wp-block-quote[style*="text-align: center"] {
< border: none;
< padding-left: 0
< }
<
912,915d899
< #content[tabindex="-1"]:focus {
< outline: 0
< }
< Other ThemesThe other themes being active only have the following CSS removed from 309,320d308
< .wp-block-quote[style*="text-align:right"], .wp-block-quote[style*="text-align: right"] {
< border-left: none;
< border-right: 4px solid #000;
< padding-left: 0;
< padding-right: 1em
< }
<
< .wp-block-quote[style*="text-align:center"], .wp-block-quote[style*="text-align: center"] {
< border: none;
< padding-left: 0
< }
< |
@@ -1456,41 +1507,64 @@ function( $selector ) { | |||
$selectors = explode( $between_selectors . ',', $split_stylesheet[ ++$i ] ); | |||
$declaration = $split_stylesheet[ ++$i ]; | |||
|
|||
// @todo The following logic could be made much more robust of PHP-CSS-Parser did parsing of selectors. See <https://github.com/sabberworm/PHP-CSS-Parser/pull/138#issuecomment-418193262>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit pick: "more robust if PHP-CSS-Parser did.."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ccf80c2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tree-shaking is getting more and more precise!
As noted in #1492 (comment), one additional CSS reduction which can be done for Twenty Nineteen is eliminate the selectors that mention
[focus-within]
since those polyfill rules will not work in AMP (since they require JS). That eliminates 1.5KB of CSS.It turns out that the infrastructure for doing attribute-based tree shaking was implemented in #1959 since it now checks if certain attributes are present on the page to know whether or not a given AMP component will be needed (e.g.
dock
foramp-video-docking
). With this in hand, the CSS selector parsing can be extended to also extract the attribute names that are referenced in the selector and then at runtime check if any of those attributes exist in the document.Note that the attribute selector tree shaking only considers if an attribute is mentioned. It does not check the value of the attribute. In other words, consider this CSS:
Only the last rule with the selector
span[data-punctuation]
would be tree-shaken. The others remain because thevalue
is not taken into consideration. The reason is that the value, especially in the case ofamp-bind
, can be mutated at runtime (see bindings); this is also the case for the[hidden]
selector since it and other such boolean attributes can be added/removed by AMP at runtime.Results
Here are the savings for each core theme, along with other core styles also being included:
As expected, the greatest savings is for Twenty Nineteen, where the
focus-within
rules are removed.👉 Interestingly, with this change, the core theme with the most CSS after tree shaking is now Twenty Fifteen. Previously it was Twenty Nineteen. Twenty Sixteen remains in third place, followed by Twenty Seventeen.
With this along with #2079, the amount of CSS for Twenty Nineteen has gone down 4.6KB (-11.07%).
Here is the script (beware that running will clear out all widgets on an install):
Here is how I invoked:
lando ssh -c "./check-tree-shaker-savings.sh https://wordpressdev.lndo.site/2019/04/09/welcome-to-the-gutenberg-editor-4/?amp"
Todo
hidden
), considering which attributes that AMP can add/remove.See #1492.