Comments (3)
Yeah, I should have explained: TailwindCSS is a utility-first (aka. functional, atomic) CSS library. It adds some useful custom directives (e.g. @apply
) that need to be converted to vanilla CSS syntax by PostCSS before the CSS can be used.
TailwindCSS generates a LOT of utility classes (to make it easy to prototype quickly) and recommends using PurgeCSS to remove any that are unused.
To use TailwindCSS with Gatsby v2, you just use gatsby-plugin-postcss
and add TailwindCSS to your postcss pipeline (before autoprefixer
):
gatsby-config.js
// ...
plugins: [
`gatsby-plugin-postcss`,
]
// ...
postcss.config.js
// ...
module.exports = {
plugins: [
// ...
require(`tailwindcss`)(`./src/styles/tailwind.js`),
require(`autoprefixer`)()
]
}
// ...
The above is enough to get TailwindCSS working, but doesn't solve the class-bloat issue, which is where PurgeCSS comes in.
Ideally, PurgeCSS will only run in production (so it doesn't slow down HMR in development). Here's the code (provided by @jquense in this issue) that achieves that in Gatsby v2:
gatsby-node.js
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: [],
whitelistPatterns: [/html/, /body/]
}
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage.includes(`develop`)) return
// Add PurgeCSS in production
if (stage.includes(`build`)) {
actions.setWebpackConfig({
plugins: [new PurgeCssPlugin(purgeCssConfig)]
})
}
}
With this setup, I'm basically good to go on my end, but I wanted to share it in case it helps gatsby-plugin-purgecss
become a one-stop shop for anyone who wants to use PurgeCSS with Gatsby (including those who need to run PostCSS first).
Let me know if anything above could be improved and feel free to use anything that might help this project.
from gatsby-plugin-purgecss.
@ooloth Sorry for the late action.
This plugin will now run after postcss (added in v3)
Tailwind is now supported using the tailwind: true
option. Please try it out, your feedback will be appreciated.
from gatsby-plugin-purgecss.
plugin cannot be used with postcss
It does not work with postcss syntax based plugins like postcss-preset-env
. But something like autoprefixer should and does work (if you modify the gatsby webpack config). I don't know if tailwind does css syntax modifications so I can't comment if that is the issue.
Also it may not work with gatsby-plugin-postcss as it did not exist when I was making this plugin and I have not checked it out.
Could you describe exactly what error did you face?
Is there a reason for this order?
I initially started with running this plugin after postcss. But at the time, how to include postcss plugins in Gatsby 2 (alpha) wasn't clear. I don't remember the details but this plugin would fail parsing files after postcss, not always but on some projects.
But now that the code is more stable, debugging is easier and gatsby-plugin-postcss
is available, I'll look at it again.
Do you have any Gatsby project repo with tailwind setup (as a postcss plugin) that I could use?
from gatsby-plugin-purgecss.
Related Issues (20)
- Gatsby 5 Peer Dependency Support? HOT 3
- Remove css classes that are not present in the rendered html file HOT 1
- Support for reactstrap or third party react elements HOT 2
- Using Tailwind with new extensions, like MDX HOT 8
- how to whitelist a Loadable component ? HOT 3
- Remove css classes from a theme HOT 2
- Option to run against rendered html HOT 1
- PurgeCss is not working with tailwindui HOT 12
- classes added by clsx not in css HOT 2
- Rerun purgecss on changes during development HOT 1
- How to specify custom tailwind config file? HOT 2
- Invalid syntax error HOT 1
- Disable print with the tailwind option HOT 6
- Not understanding how to configure content option. Documentation unclear HOT 2
- Purge specific files is ignoring with build command HOT 1
- className styles are purged if `purge` object is present in tailwind.config.js HOT 1
- TypeScript support HOT 2
- purgeCSSOptions.content override is not working HOT 1
- Dependency Dashboard
- compatibility with Gatsby 4 update? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gatsby-plugin-purgecss.