Starter files for a Hugo theme with Tailwindcss
Starter files for a Hugo theme with Tailwind CSS.
devor
buildenvironment
build, but not in
dev
Live long and code.
This theme is a starter setup theme to aid in developing Hugo themes using the Tailwind CSS framework. It is not a standalone theme ready to use.
Make sure to install
postcss-cliand
autoprefixerglobally in your environment, as Hugo Pipe’s PostCSS requires it. This is mentioned in the Hugo Docs.
npm install -g postcss-cli npm install -g autoprefixer
Make sure to use a minimum Hugo version of v0.69.0 and above.
Set the
writeStatsoption in your Hugo
configfile, so that purging of CSS classes works in production. See
/exampleSite/config.tomlas a guideline.
[build] writeStats = true
git clone https://github.com/dirkolbrich/hugo-theme-tailwindcss-starter new-theme-name
cd new-theme-name rm -rf .git git init
npm install
config.tomlfile in
exampleSite/to reflect the
new-theme-name
# in config.toml theme = "new-theme-name" # your new theme name here
exampleSite
hugo server -s exampleSite --themesDir=../.. --disableFastRender
hugo new site new-site
cd new-site/themes git clone https://github.com/dirkolbrich/hugo-theme-tailwindcss-starter new-theme-name
cd new-theme-name rm -rf .git npm install
config.tomlfile in
new-site/to reflect the new-theme-name
# in config.toml theme = "new-theme-name" # your new theme name here
cd new-site hugo server --disableFastRender
Your content should go into
new-site/content, the development of the site layout is done within
new-site/themes/new-theme-name/layout.
Included are the following helpers for the development phase (not visible in production):
/partials/dev-parameters.html, which shows basic Hugo page parameters
/partials/dev-size-indicator.html, which displays a floating circle in the upper right corner to indicate the Tailwind CSS responsive breakpoints
If you don't need any of these helpers anymore, just delete the corresponding line from
/layouts/_default/baseof.html.
If you use this starter theme and want to deploy your site to Netlify, you MAY encounter a build error which contains the following line:
ERROR {your deploy time here} error: failed to transform resource: POSTCSS: failed to transform "css/styles.css" (text/css): PostCSS not found; install with "npm install postcss-cli". See https://gohugo.io/hugo-pipes/postcss/
That is, Netlify doesn't know the
npmdependencies of this starter theme yet. For this to fix, please add a
package.jsonfile to the root of your repo with the content:
{ "name": "my-site", "version": "0.0.1", "description": "that is my-site", "repository": "https://github.com/you/my-site", "license": "MIT", "devDependencies": { "@fullhuman/postcss-purgecss": "^3.1.3", "@tailwindcss/typography": "^0.3.1", "autoprefixer": "^10.2.0", "postcss": "^8.2.3", "postcss-cli": "^8.3.1", "postcss-import": "^14.0.0", "tailwindcss": "^2.0.2" }, "browserslist": [ "last 1 version", "> 1%", "maintained node versions", "not dead" ] }
This introduces the dependencies Tailwind CSS and PostCSS need, Netlify will run the installation automatically on deploy.
To make the distinction between
developmentand
productionenvironments work, add an environment variable
HUGO_ENV = "production"to your site settings under
Settings→
Build & deploy→
Environment.
Or use a
netlify.tomlfor a file-based configuration.
Within
postcss.config.jsa
purgecssfunction is defined, which is only called based on the environment variable
HUGO_ENVIRONMENT === 'production'.
const themeDir = __dirname + '/../../';const purgecss = require('@fullhuman/postcss-purgecss')({ // see https://gohugo.io/hugo-pipes/postprocess/#css-purging-with-postcss content: ['./hugo_stats.json'], defaultExtractor: (content) => { let els = JSON.parse(content).htmlElements; return els.tags.concat(els.classes, els.ids); } })
module.exports = { plugins: [ require('postcss-import')({ path: [themeDir] }), require('tailwindcss')(themeDir + 'assets/css/tailwind.config.js'), require('autoprefixer')({ path: [themeDir], grid: true }), ...(process.env.HUGO_ENVIRONMENT === 'production' ? [purgecss] : []) ] }
During the build process Hugo Pipes checks this variable too and build the
styles.csswith some additional minification. This snippet is located in
/layouts/partials/head.html.
{{ $styles := resources.Get "css/styles.css" | postCSS (dict "config" "./assets/css/postcss.config.js") }} {{ if .Site.IsServer }} {{ else }} {{ $styles := $styles| minify | fingerprint | resources.PostProcess }} {{ end }}
Documentation for Hugo's PostCSS setup.