Would it be feasible to write a Hugo internal package to purge css files, which acts as a replacement for PurgeCSS functionality?
The current implementation of writeStats parses the build html for tags, ids and class names, but not for element attributes. The Tailwindcss-forms plugin uses the type=„“ attribute of input elements, which are purged from the css file under the current setup. see issue Collect attributes in hugo_stats.json · Issue #7560 · gohugoio/hugo · GitHub
It would be a somewhat minor rewrite to add parsing for input element attributes to Hugo writeStats, but I think that would be just a work around.
These writeStates are handed down to PurgeCSS for further handling. When running the purgecss command there is a noticeable time delay when purging from a fairly big css file, an example would be the css file generated by TailwindCSS with over 10.000 LOC. I think there could be a major speed improvement, if done natively with go and Hugo, instead of an external JS/Node process.
And wouldn’t it be nice, to include the purge command into the Hugo pipes pipeline?
{{ $css := resources.Get "css/main.css" }}
{{ $css = $css | resources.PostCSS }}
{{ if hugo.IsProduction }}
{{ $css = $css | purge | minify | fingerprint | resources.PostProcess }}
{{ end }}
<link href="{{ $css.RelPermalink }}" rel="stylesheet" />
I could not find a go package which replicates PurgeCSS, or at least handles a subset of the functionality of purging unused css from a file.
I did found some go packages which implement parsing of css files, This could be a starting point if combined with go html parsing.
Gorilla CSS3 tokenizer: GitHub - gorilla/css: A CSS3 tokenizer.
Lexer and Parser for Web formats: GitHub - tdewolff/parse: Go parsers for web formats
CSS selectors for use with the parse trees produced by the html package: GitHub - andybalholm/cascadia: CSS selector library in Go
Any thoughts on this and how to best proceed?