Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Tailwind.css working with Gatsby.js?

Has anyone managed to get Tailwind.css working with Gatsby.js?

Configuring postCSS plugins with Gatsby is a bit tricky... If anyone has managed to get Tailwind up and running with Gatsby, I'd love to know how!

like image 813
ooloth Avatar asked Jan 16 '18 00:01

ooloth


People also ask

Does Tailwind CSS use JavaScript?

Tailwind CSS is written in JavaScript and distributed as an npm package, which means you've always had to have Node. js and npm installed to use it.

Can I use Tailwind in next JS?

The quickest way to start using Tailwind CSS in your Next.js project is to use the Next.js + Tailwind CSS Example. This will automatically configure your Tailwind setup based on the official Next.js example. If you'd like to configure Tailwind manually, continue with the rest of this guide.

Can Tailwind be used with SCSS?

Since Tailwind is a PostCSS plugin, there's nothing stopping you from using it with Sass, Less, Stylus, or other preprocessors, just like you can with other PostCSS plugins like Autoprefixer.

Is Tailwind CSS difficult?

Because of the built-in classes, Tailwind CSS is quite learning-intensive. Even for experienced developers, it can be a challenge to learn how to use and fully utilize the pre-built classes. But, of course, as with any other development task, practice makes perfect.


1 Answers

I managed to get this working (including hot reloading) thanks to this post.

Basically I had to install postcss and autoprefixer additionally:

npm install autoprefixer postcss-cli

I added a postcss.config.js to the root of my Gatsby site folder with this content (tailwind.js is my Tailwind config - yours might be named differently):

const tailwindcss = require('tailwindcss');
module.exports = {
    plugins: [
        tailwindcss('./tailwind.js'),
        require('autoprefixer'),
    ],
};

Then I added a CSS watch and build script to my package.json, and included these scripts in my default develop and build scripts:

"scripts": {
  "build:css": "postcss src/layouts/index.css -o src/layouts/generated.css",
  "watch:css": "postcss src/layouts/index.css -o src/layouts/generated.css -w",
  "build": "npm run build:css && gatsby build",
  "develop": "npm run watch:css & gatsby develop",
  ...
}

(Please note the input (index.css) and output (generated.css) filenames and locations for my css are specific to my project. Feel free to use your own convention.)

Let me know, if this works for you.

like image 110
morgler Avatar answered Nov 13 '22 05:11

morgler