-
Notifications
You must be signed in to change notification settings - Fork 9
How to get Tailwind working with Gatsby? #100
Comments
Gatsby's current implementation of PostCSS is cobbled together. I posted a roundup issue about this about a month ago: Short version: it looks like Postcss dependancies included with Gatsby v1 are quite out of date. ref: Update postcss related plugins #2462 If attempting to include the tailwindcss v0.4.0 plugin in Gatsby via the current method of using its I've been debating with a colleague about taking a closer look at postcss support in Gatsby, but at the moment, we mostly stopped looking into the platform since some postcss plugins are used in our workflow/tooling. |
Thanks for the thorough reply! I'm a fan of Gatsby, so I've decided it's worth using Tailwind with it and manually building changes via the Tailwind CLI for now. It's a bit of extra effort and I'd certainly rather use postCSS to automate this step, but I created an I hope you find a good solution for your postCSS needs! |
In case anyone finds it helpful, I posted my current CLI workaround for running Sass + Tailwinds + PurgeCSS with Gatsby here on StackOverflow. If anyone can translate it into a working Webpack version for Gatsby, I'd be thrilled! |
@ooloth Is this current best practice? Is it possible to achieve this with the following plugin? https://www.npmjs.com/package/@jesses/gatsby-plugin-postcss |
@fardarter Yes, you can now run postcss on your stylesheets with Gatsby using The plugin is working for me as long as I list the postcss plugins in a To simplify the setup, I switched from Sass to CSS + a few postcss plugins that simulate Sass-like features. This made it easier to guarantee the "Sass"-style processing runs before the Tailwind processing (using the order of the plugins in If you prefer to keep using Sass, you may be able to simply use In case it helps, here's my updated setup (CSS + PostCSS + PurgeCSS) that is working with Gatsby v2 and allows me to use Sass-like features with Tailwind: npm i gatsby-plugin-postcss precss tailwindcss autoprefixer purgecss-webpack-plugin // gatsby-config.js
...
plugins: [
`gatsby-plugin-postcss`,
...
]
... // postcss.config.js
/*
PreCSS run the following plugins (in this order):
1. postcss-extend-rule
2. postcss-advanced-variables
3. postcss-preset-env
4. postcss-atroot
5. postcss-property-lookup
6. postcss-nested
*/
module.exports = {
plugins: [
require(`precss`),
require(`tailwindcss`)(`./src/styles/tailwind.js`),
require(`autoprefixer`)()
]
} // components/Base.js
import '../styles/index.css' // file that imports all other stylesheets // gatsby-node.js
// To run PurgeCSS in production, add the following:
const PurgeCssPlugin = require(`purgecss-webpack-plugin`)
const path = require(`path`)
const glob = require(`glob`)
const PATHS = {
src: path.join(__dirname, `src`)
}
const purgeCssConfig = {
paths: glob.sync(`${PATHS.src}/**/*.js`, { nodir: true }),
extractors: [
{
// Custom extractor to allow special characters (like ":") in class names
// See: https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css-with-purgecss
extractor: class {
static extract(content) {
return content.match(/[A-Za-z0-9-_:/]+/g) || []
}
},
extensions: [`js`]
}
],
whitelist: [`class-to-whitelist`], // adjust for each project
whitelistPatterns: [/body/, /headroom/, /ReactModal/, /ril/] // adjust for each project
}
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage.includes(`develop`)) return
// Add PurgeCSS in production
// See: https://github.com/gatsbyjs/gatsby/issues/5778#issuecomment-402481270
if (stage.includes(`build`)) {
actions.setWebpackConfig({
plugins: [new PurgeCssPlugin(purgeCssConfig)]
})
}
} Hopefully that helps! If anyone can suggest ways to streamline this further (or incorporate Sass), that would be great too. |
Nice one... Any thoughts on minifying the output? ta |
@magicspon Good one! I assumed the output was minified, but you're right that it isn't. Any Gatsby or webpack people know what needs to be added here? (Gatsby v2 used Webpack v4.) |
I tried adding:
no change |
@ooloth the
babel-plugin-tailwind + emotion isn't a bad compromise |
@magicspon The @apply syntax should work just fine (it does for me with the setup above). That @apply error (which I run into as well) means that the “block” class doesn’t exist or hasn’t been imported properly. |
that was my first thought... but |
this works: @tailwind utilities;
.test {
@apply .text-blue;
} import React, { Component } from 'react'
import './Panel.css'
export default class Panel extends Component {
render() {
return <div className="test">hello world</div>
}
} but then i have multiple style tags with duplicate code... i'm possibly going terribly wrong somewhere!! |
What works for me is to have all of my CSS files imported by a single /* index.css */
@import './base/index';
@import './plugins/index';
@import './components/index';
@import './utilities/index';
@import './new'; ...where each /* base/index.css, for example */
@import './font-face';
@import './tailwind-preflight';
@import './reset';
@import './a11y'; Make sure the /* base/_tailwind-preflight.css, for example */
@tailwind preflight; /* the only line in the file */ And then just import the main // components/Layout.js
import '../styles/index.css' /* the only CSS import in the entire project */ As long as your CSS imports are set up correctly (it took me some time to fix problems with mine) and you are only importing one CSS file into your JS, you should end up with a single style tag that includes all of your CSS in the desired order. With this setup, I'm able to use The PurgeCSS additions to Hope that helps! |
Aces...
This is where I was going wrong. Thanks |
For minifying css add the const OptimizeCSSAssetsPlugin = require(`optimize-css-assets-webpack-plugin`)
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage.includes(`build`)) {
actions.setWebpackConfig({
plugins: [
new PurgeCssPlugin(purgeCssConfig),
],
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin(),
],
},
})
}
} |
I believe there is a pull request to add this to the core. |
You are right @magicspon |
Surprised this isn't a documented workflow yet. Has anyone got a tailwind/purge/sass workflow going yet? |
example of using tailwind/purge here: credit to @ooloth |
@magicspon Thanks for having a look. I actually started tinkering with your spon-postcss plugin. As far as the gatsby-starter you just posted - where is the sass flavor here? |
@Devin-Chaves alas... i didn't end up using scss with the craftcms starter. |
Has anyone managed to get Tailwind 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!
The text was updated successfully, but these errors were encountered: