Optimize generated CSS by removing unused selectors (#2008)
Hugo generates stats about the HTML elements, IDs and classes that can be found in the website, and we post-process the rendered CSS with postcss-purgecss that uses those stats to remove unused selectors. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
parent
54d872e19b
commit
1accb9e93f
8 changed files with 1240 additions and 279 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -14,3 +14,4 @@ _rendered.rst
|
||||||
/spec/
|
/spec/
|
||||||
changelogs/rendered.*
|
changelogs/rendered.*
|
||||||
.hugo_build.lock
|
.hugo_build.lock
|
||||||
|
hugo_stats.json
|
||||||
|
|
1
changelogs/internal/newsfragments/2008.clarification
Normal file
1
changelogs/internal/newsfragments/2008.clarification
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Optimize generated CSS by removing unused selectors.
|
|
@ -1,3 +1,5 @@
|
||||||
|
# Default settings.
|
||||||
|
|
||||||
baseURL = "/"
|
baseURL = "/"
|
||||||
title = "Matrix Specification"
|
title = "Matrix Specification"
|
||||||
|
|
6
config/production/hugo.toml
Normal file
6
config/production/hugo.toml
Normal file
|
@ -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
layouts/partials/head-css.html
Normal file
38
layouts/partials/head-css.html
Normal file
|
@ -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 -}}
|
||||||
|
|
||||||
|
{{- /**/ -}}
|
1449
package-lock.json
generated
1449
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -19,9 +19,10 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/matrix-org/matrix-spec#readme",
|
"homepage": "https://github.com/matrix-org/matrix-spec#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"autoprefixer": "^10.4.2",
|
"@fullhuman/postcss-purgecss": "^6.0.0",
|
||||||
|
"autoprefixer": "^10.4.20",
|
||||||
"node-fetch": "^2.6.7",
|
"node-fetch": "^2.6.7",
|
||||||
"postcss-cli": "^9.1.0",
|
"postcss": "^8.4.49",
|
||||||
"postcss": "^8.4.6"
|
"postcss-cli": "^11.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
postcss.config.js
Normal file
15
postcss.config.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// Remove unused CSS selectors.
|
||||||
|
const purgecss = require('@fullhuman/postcss-purgecss')({
|
||||||
|
// Use stats generated by Hugo.
|
||||||
|
content: [ './hugo_stats.json' ],
|
||||||
|
defaultExtractor: (content) => {
|
||||||
|
let els = JSON.parse(content).htmlElements;
|
||||||
|
return els.tags.concat(els.classes, els.ids);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
plugins: [
|
||||||
|
...(process.env.HUGO_ENVIRONMENT === 'production' ? [ purgecss ] : [])
|
||||||
|
]
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue