Skip to content
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

Optimize generated CSS by removing unused selectors #2008

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ _rendered.rst
/spec/
changelogs/rendered.*
.hugo_build.lock
hugo_stats.json
1 change: 1 addition & 0 deletions changelogs/internal/newsfragments/2008.clarification
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize generated CSS by removing unused selectors.
2 changes: 2 additions & 0 deletions config.toml → config/_default/hugo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Default settings.

baseURL = "/"
title = "Matrix Specification"

Expand Down
6 changes: 6 additions & 0 deletions config/production/hugo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Settings only required when the website is built for production.

# Enable stats to use them to optimize the CSS.
[build]
[build.buildStats]
enable = true
38 changes: 38 additions & 0 deletions layouts/partials/head-css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- /*

A modified version of the head-css.html partial of Docsy, that adds a call to
`resources.PostProcess`, allowing post-processing of the generated CSS to
remove unused selectors.

*/ -}}

{{ $scssMain := "scss/main.scss" -}}
{{ $css := resources.Get $scssMain
| toCSS (dict "enableSourceMap" (not hugo.IsProduction)) -}}

{{/* NOTE: we only apply `postCSS` in production or for RTL languages. This
makes it snappier to develop in Chrome, but it may look sub-optimal in other
browsers. */ -}}

{{ if eq .Site.Language.LanguageDirection "rtl" -}}
{{ $css = $css
| postCSS (dict "use" "autoprefixer rtlcss" "noMap" true)
| resources.Copy (replace $scssMain "." ".rtl.") -}}
{{ else if hugo.IsProduction -}}
{{ $css = $css | postCSS -}}
{{ end -}}

{{ if hugo.IsProduction -}}
{{ $css = $css | minify | fingerprint | resources.PostProcess -}}
<link rel="preload" href="{{ $css.RelPermalink }}" as="style" integrity="{{ $css.Data.Integrity }}" crossorigin="anonymous">
{{ end -}}

{{ with $css -}}
<link href="{{ .RelPermalink }}" rel="stylesheet"
{{- with .Data.Integrity }} integrity="{{ . }}" crossorigin="anonymous"{{ end -}}
>
{{ else -}}
{{ errorf "Resource not found or error building CSS: %s" $scssMain -}}
{{ end -}}

{{- /**/ -}}
Loading
Loading