Minify stylesheets for twentytwentyone theme.#10860
Minify stylesheets for twentytwentyone theme.#10860rutviksavsani wants to merge 14 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @1178653+wordpress-develop-pr-bot[bot]@users.noreply.github.com. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
This PR implements CSS minification for the Twenty Twenty-One theme as a test case before applying the approach to all classic themes. The implementation adds conditional loading of minified stylesheets based on the SCRIPT_DEBUG constant.
Changes:
- Added
cssnanodependency for CSS minification - Implemented conditional loading logic in
functions.phpto serve minified CSS in production - Added build script for CSS minification
- Updated CSS files with developer documentation comments
Reviewed changes
Copilot reviewed 6 out of 10 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
style.min.css |
New minified stylesheet generated by build process |
functions.php |
Conditional loading logic for minified CSS based on SCRIPT_DEBUG |
package.json / package-lock.json |
Added cssnano dependency and build:minify script |
style.css / style-rtl.css / ie.css / SCSS source |
Added documentation comments about minification |
Files not reviewed (1)
- src/wp-content/themes/twentytwentyone/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| /* | ||
| * IMPORTANT: This file is only served on the frontend when `SCRIPT_DEBUG` is enabled; | ||
| * in most instances, the `style.min.css` file will be served. This theme uses SCSS for styles. |
There was a problem hiding this comment.
The comment states "in most instances, the style.min.css file will be served" but this is only accurate when SCRIPT_DEBUG is false (default production mode). The comment should clarify that style.min.css is served in production when SCRIPT_DEBUG is not enabled.
| * in most instances, the `style.min.css` file will be served. This theme uses SCSS for styles. | |
| * in production, when `SCRIPT_DEBUG` is not enabled, the `style.min.css` file will be served. This theme uses SCSS for styles. |
| $suffix = ''; | ||
| $can_use_minified = ! is_child_theme() || file_exists( get_stylesheet_directory() . '/style.min.css' ); | ||
|
|
||
| if ( ! SCRIPT_DEBUG && $can_use_minified ) { |
There was a problem hiding this comment.
The logic checks if the child theme has a style.min.css file, but doesn't verify if it's up-to-date or valid. Consider whether stale or corrupted minified files in child themes could cause issues. Additionally, the variable name $can_use_minified suggests a permission check, but it's actually a file existence check - consider renaming to $minified_file_exists for clarity.
| $suffix = ''; | |
| $can_use_minified = ! is_child_theme() || file_exists( get_stylesheet_directory() . '/style.min.css' ); | |
| if ( ! SCRIPT_DEBUG && $can_use_minified ) { | |
| $suffix = ''; | |
| // Decide whether a minified stylesheet can be safely used. | |
| $minified_file_is_valid = ! is_child_theme(); | |
| if ( is_child_theme() ) { | |
| $minified_file_path = get_stylesheet_directory() . '/style.min.css'; | |
| $unminified_file_path = get_stylesheet_directory() . '/style.css'; | |
| // Basic validity: file must exist, be readable, and non-empty. | |
| $minified_file_is_valid = file_exists( $minified_file_path ) | |
| && is_readable( $minified_file_path ) | |
| && 0 !== filesize( $minified_file_path ); | |
| // If both minified and unminified files exist, ensure the minified file is not older. | |
| if ( $minified_file_is_valid && file_exists( $unminified_file_path ) ) { | |
| $minified_file_is_valid = filemtime( $minified_file_path ) >= filemtime( $unminified_file_path ); | |
| } | |
| } | |
| if ( ! SCRIPT_DEBUG && $minified_file_is_valid ) { |
There was a problem hiding this comment.
I don't think this is valid. I don't core does any readability or non-zero size checks for any theme stylesheets.
There was a problem hiding this comment.
The existing theme stylesheets do not have a fallback option if something is wrong (file is not readable, empty, missing, inaccurate, etc.). Minified files would have an alternate version that could be fetched in the case of some predictable failures.
Checking the modified time would not accurately determine whether the minified file is outdated in relation to the unminified stylesheet. I could update the styles locally, compile all stylesheets, and then upload the minified files via FTP before their unminified versions.
I appreciate that you checked if the site has a child theme in this PR, but Twenty Twenty-One does not use get_stylesheet_directory(). It uses get_template_directory_uri(), which refers to the twentytwentyone directory regardless of whether the active theme is T21 or a child theme.
Another important note about T21 is that multiple stylesheets affect the front end:
style.cssstyle-rtl.css, which replacesstyle.cssin right-to-left languagesie.css(though IE support might be removed soon: Trac 64590)print.css
The notice in file-header.scss (or possibly in style.scss) should not mention style.min.css because the comment is inserted into three different compiled stylesheets, and their minified versions would have different file names.
There was a problem hiding this comment.
So in other words, since the code here is using get_template_directory_uri() and not get_stylesheet_directory_uri(), we can eliminate the $can_use_minified variable, correct?
| "build:ie": "postcss style.css -o assets/css/ie.css", | ||
| "build:ie-editor": "postcss assets/css/style-editor.css -o assets/css/ie-editor.css", | ||
| "build:stylelint": "stylelint **/*.css --fix --config .stylelintrc-css.json", | ||
| "build:minify": "postcss style.css --use cssnano -o style.min.css --no-map", |
There was a problem hiding this comment.
The build script uses --no-map flag which prevents source map generation for the minified CSS. While this is acceptable for production, consider documenting this decision or providing a separate script that generates source maps for debugging purposes.
| "build:minify": "postcss style.css --use cssnano -o style.min.css --no-map", | |
| "build:minify": "postcss style.css --use cssnano -o style.min.css --no-map", | |
| "build:minify:debug": "postcss style.css --use cssnano -o style.min.css", |
There was a problem hiding this comment.
I don't believe we do this for the block themes which have had minification done. For debugging, it is more common to use SCRIPT_DEBUG as opposed to rely on a source map (although maybe we should revisit that in the future).
There was a problem hiding this comment.
Oh wait, but there is a style.css.map committed in this PR?
There was a problem hiding this comment.
Oh, interesting. Twenty Nineteen actually has a source map: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-content/themes/twentynineteen/style.css.map
There was a problem hiding this comment.
I'm confused. The command has --no-map and yet there is a style.css.map present?
There was a problem hiding this comment.
This PR is for Twenty Twenty-One, which has source map files for its current stylesheets. The --no-map flag prevents the build command from adding a giant 240KB sourceMappingURL comment in style.min.css (resulting in a file more than twice the original size).
Regarding Twenty Nineteen, its early development involved a source map file that appears in trunk, but it was never updated.
There was a problem hiding this comment.
@sabernhardt So in reality we should the source map file for Twenty Nineteen should actually be deleted?
There was a problem hiding this comment.
I don't like deleting files, but Twenty Nineteen's outdated .map cannot be very helpful. In the IE-related PR, I proposed emptying the ie.css.map file.
|
@westonruter As suggested in the other PR for minification of stylesheets for themes, Here I have created a separate PR for TT1. |
|
cc @sabernhardt |
|
@rutviksavsani With r61628 (05e54af), IE support has been removed from the theme and this has introduced merge conflicts. Would you address? |
…nify-twentytwentyone
c85a045 to
efc129c
Compare
westonruter
left a comment
There was a problem hiding this comment.
FYI: We're two days away from the beta1 cut-off for 7.0.
There was a problem hiding this comment.
The minified file shouldn't actually be committed to the repo. It should be ignored as was done for Twenty Twenty-Five and Twenty Twenty-Two.
sabernhardt
left a comment
There was a problem hiding this comment.
Please minify all the front-end stylesheets, and restore the deleted files.
Note: these changes will not be ready to include in the next release, so you do not need to rush updating the pull request.
There was a problem hiding this comment.
I purposely did not delete files such as ie.css and ie.css.map in case something still points to them (for example, in a child theme). Please restore the small files.
| * IMPORTANT: This file is only served on the frontend when `SCRIPT_DEBUG` is enabled; | ||
| * in production, when `SCRIPT_DEBUG` is not enabled, the minified .css file will be served. This theme uses SCSS for styles. | ||
| * After making changes to SCSS files, run `npm run build` in the theme directory to compile | ||
| * SCSS to CSS and regenerate the minified version. |
There was a problem hiding this comment.
"In production" can start a new sentence, and the lines can wrap better.
| * IMPORTANT: This file is only served on the frontend when `SCRIPT_DEBUG` is enabled; | |
| * in production, when `SCRIPT_DEBUG` is not enabled, the minified .css file will be served. This theme uses SCSS for styles. | |
| * After making changes to SCSS files, run `npm run build` in the theme directory to compile | |
| * SCSS to CSS and regenerate the minified version. | |
| * IMPORTANT: This file is only served on the frontend when `SCRIPT_DEBUG` is enabled. | |
| * In production, when `SCRIPT_DEBUG` is not enabled, the minified .css file will be served. | |
| * This theme uses SCSS for styles. After making changes to SCSS files, run `npm run build` | |
| * in the theme directory to compile SCSS to CSS and regenerate the minified version. |
| * @return void | ||
| */ | ||
| function twenty_twenty_one_scripts() { | ||
| $theme_version = wp_get_theme()->get( 'Version' ); |
There was a problem hiding this comment.
Right now, the $theme_version variable is only used once. I would just put wp_get_theme()->get( 'Version' ) back within wp_enqueue_style(). However, if the variable is preferred, it should be used throughout the twenty_twenty_one_scripts() function.
| "build:dark-rtl": "rtlcss assets/css/style-dark-mode.css assets/css/style-dark-mode-rtl.css", | ||
| "build:print": "sass assets/sass/07-utilities/print.scss:assets/css/print.css --style=expanded --source-map", | ||
| "build:stylelint": "stylelint **/*.css --fix --config .stylelintrc-css.json", | ||
| "build:minify": "postcss style.css --use cssnano -o style.min.css --no-map", |
There was a problem hiding this comment.
Create minified copies of all (5) stylesheets.
| "build:minify": "postcss style.css --use cssnano -o style.min.css --no-map", | |
| "build:minify": "postcss style.css --use cssnano -o style.min.css --no-map", | |
| "build:minify-rtl": "postcss style-rtl.css --use cssnano -o style-rtl.min.css --no-map", | |
| "build:minify-print": "postcss assets/css/print.css --use cssnano -o assets/css/print.min.css --no-map", | |
| "build:minify-dark-mode": "postcss assets/css/style-dark-mode.css --use cssnano -o assets/css/style-dark-mode.min.css --no-map", | |
| "build:minify-dark-rtl": "postcss assets/css/style-dark-mode-rtl.css --use cssnano -o assets/css/style-dark-mode-rtl.min.css --no-map", |
| wp_enqueue_style( 'twenty-twenty-one-style', get_template_directory_uri() . '/style' . $suffix . '.css', array(), $theme_version ); | ||
|
|
||
| // RTL styles. | ||
| wp_style_add_data( 'twenty-twenty-one-style', 'rtl', 'replace' ); |
There was a problem hiding this comment.
The RTL 'replace' would make sites in right-to-left languages try to fetch a style.min-rtl.css file, which needs to be built in package.json and should include the 'suffix' data for a filename like style-rtl.min.css. The twenty_twenty_one_scripts() function can also include @since documentation and fetch a minified print stylesheet:
* @since Twenty Twenty-One 1.0
* @since Twenty Twenty-One 2.8 Removed Internet Explorer support.
* @since Twenty Twenty-One 2.9 Added minified stylesheets.
*
* @return void
*/
function twenty_twenty_one_scripts() {
$suffix = ( ! SCRIPT_DEBUG ) ? '.min' : '';
// The standard stylesheet.
wp_enqueue_style( 'twenty-twenty-one-style', get_template_directory_uri() . "/style$suffix.css", array(), wp_get_theme()->get( 'Version' ) );
// RTL styles.
wp_style_add_data( 'twenty-twenty-one-style', 'rtl', 'replace' );
wp_style_add_data( 'twenty-twenty-one-style', 'suffix', $suffix );
// Print styles.
wp_enqueue_style( 'twenty-twenty-one-print-style', get_template_directory_uri() . "/assets/css/print$suffix.css", array(), wp_get_theme()->get( 'Version' ), 'print' );
And then fetch the minified dark mode stylesheets like this:
/**
* Enqueues scripts and styles.
*
* @since Twenty Twenty-One 1.0
* @since Twenty Twenty-One 2.9 Added minified stylesheets.
*
* @return void
*/
public function enqueue_scripts() {
if ( ! $this->switch_should_render() ) {
return;
}
$suffix = ( ! SCRIPT_DEBUG ) ? '.min' : '';
$url = get_template_directory_uri() . "/assets/css/style-dark-mode$suffix.css";
if ( is_rtl() ) {
$url = get_template_directory_uri() . "/assets/css/style-dark-mode-rtl$suffix.css";
}
wp_enqueue_style( 'tt1-dark-mode', $url, array( 'twenty-twenty-one-style' ), wp_get_theme()->get( 'Version' ) );
}
This PR implements the Minification for TwentyTwentyOne theme initially to test out before we convert all of them in #10818
Trac ticket: https://core.trac.wordpress.org/ticket/64109
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.