Minify stylesheets for twentytwentyone theme.#10860
Minify stylesheets for twentytwentyone theme.#10860rutviksavsani wants to merge 9 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.
| "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.
|
@westonruter As suggested in the other PR for minification of stylesheets for themes, Here I have created a separate PR for TT1. |
|
cc @sabernhardt |
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.