CARVIEW |
Opened 7 months ago
Last modified 24 hours ago
#63012 accepted enhancement
Bundled themes: Stylesheets should be minified in block themes
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.9 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Bundled Theme | Keywords: | has-patch commit |
Focuses: | css, performance | Cc: |
Description (last modified by westonruter)
Since core has CSS minification as part of the build process, shouldn't this be used by themes as well? This would help address things like #47925, where the stylesheet for T19 is 224K but after going through cssmin it is reduced to 196K. It doesn't really make sense for themes to serve the WP theme metadata CSS comment block to browsers. I should think that all
.css
files in a theme should also have a corresponding.min.css
file. Maybe minification has been discouraged in the past to facilitate authors forking a theme and making changes to the CSS, with there being an unexpected result where the minified CSS wouldn't also be updated. Nevertheless, themes like T19, T20, and T21 are already using a build step for the CSS so authors shouldn't be directly modifying thestyle.css
file directly anyway. They should be using the build process to re-generate the CSS instead.
Minifying the CSS will facilitate allowing the stylesheets to be inlined, for example in #63007. Without minification, enqueued CSS will more quickly reach the
styles_inline_size_limit
.
And as noted by @karmatosed:
I would also support minification for themes if this is correctly documented. I think historically it has been as you noted not done because of confusion where to update. However, in many real world situations this is part of the development process.
And as advised by @sabernhardt:
Classic themes from Twenty Ten to Twenty Twenty call
get_stylesheet_uri()
for their main stylesheets, so a child theme'sstyle.css
would replace its parent stylesheet.
I expect that a patch would need an
is_child_theme()
check to avoid looking for a minified file in the child theme directory (and/or printing the parent stylesheet inline).
Blocking:
Change History (30)
#1
@westonruter
7 months ago
#2
@poena
7 months ago
Using the core tool is a no-go because most people will not have a development version of WordPress.
The themes also support versions below 6.9, so if only the core tools are updated, the tools won't be available for all.
#3
@poena
7 months ago
Or, could WordPress minify the files without the files needing to be in the theme?
Could it be introduced on a core level, and only flagged in the theme?
#4
@westonruter
7 months ago
@poena This is about using the same build process as core already has for minifying CSS files, but just extending it to bundled themes where currently core is just minifying CSS in wp-admin
and wp-includes
. By using a development version of WordPress, do you mean running from src
without doing a build? This would still be supported since with SCRIPT_DEBUG
the non-minified files would be used, thus not necessitating doing a build (via core's existing grunt build
).
The minified CSS files would be built with WordPress, and the static files would be shipped with the themes. There would not be any dynamic minification process, so there's no back-compat issue.
You can see an example of the scope of changes, specifically for Twenty Twenty-Two and Twenty Twenty-Five in this PR for #63007. The scope of changes for that ticket is primarily about being able to inline the small style.css
files (the addition of the calls to wp_style_add_data()
in the PR), but minification is included there because there is a limit to the amount of CSS that can be inlined.
#5
@poena
7 months ago
I don't think we are understanding each other.
For a core committer, using the existing (updated) script to minify the files is not a problem, besides the increased maintainance burden.
But users who are using a version of WordPress where no tools are included, what they will experience is that they are making changes to style.css and the changes are not being applied, because they will not have the required knowledge nor tools.
#6
@westonruter
7 months ago
@poena I've replied on the other ticket: https://core.trac.wordpress.org/ticket/63007#comment:14
This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.
7 months ago
#8
@westonruter
7 months ago
Re-posting relevant parts of my comment from #63007 in reply to @poena:
When the ticket was opened, it included this comment:
"I'm not sure if this is the right place to introduce the minification, as perhaps it should be part of the theme's own build process." [...]
But then without further discussion it seems to have been decided that it must be the minification script that is in core. Which, would require knowledge of this script. I believe that is not an incorrect claim.
Yeah, I included minification of the style.css
files for the block themes here because when they are inlined they should be minified to ensure they don't exceed the styles_inline_size_limit
limit. So perhaps #63012 should be addressed first as a dependency for this ticket.
I added this as one point of my comment, in addition to documentation. [...]
And the comment about documentation has not been addressed at all.
In regards to documentation, the documentation for testing a patch already instructs to run grunt build
. So any additional documentation specifically for testing themes should include this if it doesn't already.
Secondly, I described a scenario where a user repurposes the theme and adds CSS to style.css.
It does not matter if the user uses a text editor or the built in theme file editor.
What matters is that style.css is not enqueued, instead the minified files is. And the user does not have knowledge of that they need to edit the minified files, or how, or knowledge about script debug.
So the users changes are not working, and it is not a positive experience for them.
If someone forks a theme and modifies the style.css
then this indeed would be an issue. I described this in a previous comment on #49665 where I wondered if the forking scenario was a reason why minification hasn't been done in the past. Nevertheless, there are themes today that already have a build step for their CSS, namely Twenty Nineteen and Twenty Twenty-One. So technically if someone forks those themes they already should not be using the theme file editor to edit the style.css
file directly but rather to edit the underlying SASS files and then run npm install && npm run build
in the theme. For themes that use SASS and for those which have minified CSS files, there should probably be a big notice comment in the style.css
that instructs the users to run npm run build
after making a change, and that for themes using SASS that the change should be made to the SASS files directly. For minification, the comment can say something like:
/* * NOTICE: * This file is served minified to improve performance. In order to see edits * to this file on the frontend, either enable the `SCRIPT_DEBUG` constant or * run `npm run build` to regenerate `style.min.css`. Alternatively, if you do * not want to use minified version, then remove the `style.min.css` version from * being enqueued in `functions.php`. */
Every theme would need to include their own package.json
which has the npm run build
command that which would result in the minified files being generated so that they wouldn't have to rely on core's build process to generate those files.
This ticket was mentioned in Slack in #core-performance by adamsilverstein. View the logs.
7 months ago
This ticket was mentioned in Slack in #core-performance by adamsilverstein. View the logs.
4 months ago
#11
@westonruter
4 months ago
- Owner set to westonruter
- Status changed from new to accepted
#12
@westonruter
4 months ago
We discussed this today during the bi-weekly Performance bug scrub.
To facilitate the editing of CSS files in the theme file editor, we could do the CSS minification whenever a *.css
file is modified to then update the corresponding *.min.css
file. We can show a notice when editing a *.css
file when there is a corresponding *.min.css
file that the latter will be updated with a minified version of the former automatically.
The three scenarios I see for editing CSS files that would need re-minification are:
- Distributed theme stylesheets are minified automatically as part of the core build process.
- Stylesheets edited by a developer on the filesystem are minified via
npm run build
thanks to apackage.json
shipped with the theme (as is already done in T19, T20, T21). The file header comment in the CSS file must include instructions for how to re-minify. If a developer does not want to do this, they can enableSCRIPT_DEBUG
. - A stylesheet modified in the theme file editor is automatically minified to update any corresponding
*.min.css
file. This minification could be done client-side (in JavaScript) or server-side (in PHP e.g. via MyIntervals/PHP-CSS-Parser).
The automatic re-minification process might be able to be employed in the second scenario as well (when a developer edits the file), but this could be out of scope here. For example, if wp_get_environment_type()
returns local
/development
, or if wp_get_development_mode()
returns theme
/all
, then with each page load, core could read the contents of the stylesheet, compute an MD5 hash, and store this in a transient. If at the next page load the MD5 hash changes, then it could re-minify the CSS file. This would depend on there being a PHP-based CSS minifier.
Before doing any of this, I'll work on gathering metrics on what the performance gains are with minified stylesheets (and inlined too).
This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.
3 weeks ago
#14
@westonruter
3 weeks ago
- Type changed from defect (bug) to enhancement
#15
@westonruter
3 weeks ago
- Owner changed from westonruter to b1ink0
- Status changed from accepted to assigned
#16
@westonruter
3 weeks ago
#17
@westonruter
3 weeks ago
- Description modified (diff)
This ticket was mentioned in PR #10081 on WordPress/wordpress-develop by @b1ink0.
8 days ago
#18
- Keywords has-patch added
TODO:
- [x] Stylesheet minification for Twenty Twenty-Two and Twenty Twenty-Five (Block themes).
- [x] Stylesheet minification for Twenty Nineteen, Twenty Twenty, and Twenty Twenty-One themes (Classic themes with existing build setup).
- [ ] Stylesheet minification for classic themes without existing build setup.
- [ ] Use Webpack instead of Gruntfile for a more streamlined workflow.
- [ ] Update related GitHub workflow for handling stylesheet minification.
- [ ] Add server-side or client-side minification of stylesheets.
Trac ticket: https://core.trac.wordpress.org/ticket/63012
@b1ink0 commented on PR #10081:
8 days ago
#19
cc: @westonruter
@westonruter commented on PR #10081:
8 days ago
#20
@b1ink0 To make this easier to review and initially land, how about this initial PR be limited to just the block themes. To that end, could you revert the changes to all themes other than Twenty Twenty-Two and Twenty Twenty-Five? Once this is merged, we can then follow up with restoring the changes you currently have. Once we establish the pattern with the block themes, then it will be easier to then follow up with classic themes.
The highest priority is for the block themes well, since these are the ones which will be eligible to have their theme styles inlined, since they are very small. The classic themes will likely not be eligible for inlining, so the impact of minification will be less.
@westonruter commented on PR #10081:
8 days ago
#21
@westonruter commented on PR #10081:
5 days ago
#22
Can this be marked as ready for review as opposed to being a draft? It would be good to get more contributors who work on bundled themes to also look. It's looking good to me!
- Update related GitHub workflow for handling stylesheet minification.
What is this about?
@b1ink0 commented on PR #10081:
5 days ago
#23
- Update related GitHub workflow for handling stylesheet minification.
What is this about?
Need to work on the deployment workflow of themes with a build step. There is already logic for handling T19, T20, and TT1, but need to add support for TT2 and TT5. Will do it today.
#24
@westonruter
4 days ago
@poena This is largely ready for review: https://github.com/WordPress/wordpress-develop/pull/10081
#25
@westonruter
4 days ago
- Summary changed from Bundled themes: Stylesheets should be minified to Bundled themes: Stylesheets should be minified in block themes
Note the initial scope has been reduced to just the block themes, TT2 and TT5.
We can follow up with non-block themes either with an additional commit for this ticket, or open a new ticket later.
The stylesheets for block themes are the most important to minify because they are the best candidates for inlining, given they are small. The stylesheets for classic themes are much much larger, so it is unlikely they would be suitable for inlining, so minifying them would not result in the desired ideal of eliminating render-blocking external stylesheets.
This ticket was mentioned in Slack in #core by welcher. View the logs.
27 hours ago
@westonruter commented on PR #10081:
24 hours ago
#27
Need to work on the deployment workflow of themes with a build step. There is already logic for handling T19, T20, and TT1, but need to add support for TT2 and TT5.
@b1ink0 I've added these in f908cca and b83b6d0
#28
@westonruter
24 hours ago
- Keywords commit added
This is looking and testing well for me! The PR should have someone on the themes team to look it over as well.
This ticket was mentioned in Slack in #core-themes by westonruter. View the logs.
24 hours ago
#30
@westonruter
24 hours ago
- Owner changed from b1ink0 to westonruter
- Status changed from assigned to accepted
In a PR for #63007 I've added initial minification for the
style.css
in Twenty Twenty-Two and Twenty Twenty-Five. I'm not sure this is the best approach, however. I implemented it in core's overall gruntfile. Perhaps the minification should be part of each theme's build step if they have one.Also, should we try to generate source maps? Granted this isn't really necessary when
SCRIPT_DEBUG
can be enabled.