An Interest In:
Web News this Week
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
- April 12, 2024
How to use TailwindCSS 3.0 without external NPM scripts, just Hugo pipes
TailwindCSS is a utility-first CSS framework for rapid UI development and as of version 3, the JIT mode is the new default. Among other benefits, it renders a CSS file, that only contains the code, that your site is actually using -- nothing more!
1. What changed since TailwindCSS 3
Since version 3, TailwindCSS only uses the JIT compiler to do its magic. That means, that utility classes will be generated on demand instead of purging unused classes afterward. The thing with TailwindCSS is, that only the compilation result changes, not the input file. For this reason, Hugo Pipes won't work beyond the initial render pass. This behavior is perfectly fine for a production build, but it doesn't detect changes when running a Hugo development server in "watch" mode.
{{/* Doesn't work in "watch" mode */}}{{ $style := resources.Get "tailwind.css" | resources.PostCSS | resources.Minify }}
2. How to use TailwindCSS without external NPM scripts, just Hugo Pipes
First up, we use TailwindCSS as a PostCSS plugin. This is key to using Hugo's build-in resources.PostCSS
pipe. You can read how to setup PostCSS with TailwindCSS here: https://tailwindcss.com/docs/using-with-preprocessors#using-post-css-as-your-preprocessor
To make work in both production and development the "Hugo Way", I came up with a little hack. Rather than using the input file as a regular CSS resource, we can tell Hugo to treat it like a template file using the resources.ExecuteAsTemplate
pipe.
To ensure that the result always changes, we can create a random string and pass it into the CSS input template. Also, the final CSS file has the generated random string as part of its file name.
Note that this is only necessary during development. In production, a single render pass by Hugo is all it takes to work with TailwindCSS.
This is the complete code that a came up with.
{{ if .Site.IsServer }} {{ $seed := "weqklrjfmnk213409ufasdfhnlk3j4bladsfsl" }} {{ $random := delimit (shuffle (split (md5 $seed) "" )) "" }} {{ $style := resources.Get "tailwind.css" | resources.PostCSS | resources.ExecuteAsTemplate (printf "tailwind.dev.%s.css" $random) $random }} <link rel="stylesheet" href="{{ $style.RelPermalink }}">{{ else }} {{ $style := resources.Get "tailwind.css" | resources.PostCSS | resources.Minify }} <link rel="stylesheet" href="{{ $style.RelPermalink }}">{{ end }}
3. Conclusions
In this article, we discussed how to use TailwindCSS without external NPM scripts by using Hugo pipes. First, we discussed how to use the JIT mode in TailwindCSS 3.0. Next, I showed how to use Hugo pipes to treat the input file like a template so that it always triggers a re-compilation when running Hugo in "watch" mode.
4. References and further reading
Original Link: https://dev.to/jonas_duri/how-to-use-tailwindcss-30-without-external-npm-scripts-just-hugo-pipes-2lg9
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To