supports it as well. We are going to install TailwindCSS as a PostCSS plugin and then use Hugo’s PostCSS
pipe to integrate it to our blog. With this plan in mind, let’s get started.
We start with following official docs in a blog/
folder.
$ npm install --save-dev tailwindcss postcss autoprefixer
.gitignore
file for Node.js projects$ npx tailwindcss init --postcss
--postcss
option to initialize a postcss.config.js
file as well.content
files located in blog/src/layouts
folder.diff --git a/blog/tailwind.config.js b/blog/tailwind.config.js
index 32e3abd..7b5a700 100644
--- a/blog/tailwind.config.js
+++ b/blog/tailwind.config.js
@@ -1,6 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
- content: [],
+ content: ["./src/layouts/**/*.html"],
theme: {
extend: {},
},
$ mkdir -p blog/src/assets
$ touch !$/main.css
blog/src/assets/main.css
diff --git a/blog/src/assets/css/main.css b/blog/src/assets/css/main.css
new file mode 100644
index 0000000..b5c61c9
--- /dev/null
+++ b/blog/src/assets/css/main.css
@@ -0,0 +1,3 @@
+@tailwind base;
+@tailwind components;
+@tailwind utilities;
.editorconfig
, .pre-commit-config.yaml
and .prettierrc.yaml
to enable support for css
file type.Now we have installed TailwindCSS, we are ready to start configuring our blog to use it via PostCSS
pipe.
From the docs
Hugo Pipe’s PostCSS requires the postcss-cli JavaScript package to be installed in the environment (npm install -g postcss postcss-cli) along with any PostCSS plugin(s) used (e.g., npm install -g autoprefixer).
Install postcss-cli
$ npm install --save-dev postcss-cli
Add link
tag with our processed CSS to blog/src/layouts/partials/head.html
diff --git a/blog/src/layouts/partials/head.html b/blog/src/layouts/partials/head.html
index b9f74a6..556a0cf 100644
--- a/blog/src/layouts/partials/head.html
+++ b/blog/src/layouts/partials/head.html
@@ -3,4 +3,6 @@
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ .Site.Title }}</title>
+ {{ $css := resources.Get "css/main.css" | resources.PostCSS }}
+ <link rel="stylesheet" href="{{ $css.RelPermalink }}" />
Add scripts
command to package.json
to run hugo in NPM environment. By default, Hugo will search for node_modules/
From Hugo 0.78.1 the start directory for resolving NPM packages (aka. packages that live inside a node_modules folder) is always the main project folder.
but because of our project structure we need to run hugo as npm command. We will use start
as command name because npm has shorthand for it.
diff --git a/blog/package.json b/blog/package.json
index 3f40e6d..5b5d864 100644
--- a/blog/package.json
+++ b/blog/package.json
@@ -1,4 +1,7 @@
{
+ "scripts": {
+ "start": "hugo --source src server --baseURL http://localhost/"
+ },
"devDependencies": {
"autoprefixer": "^10.4.7",
"postcss": "^8.4.14",
Profit!
We now have a blog that uses TailwindCSS.