Giter Club home page Giter Club logo

Comments (3)

ooloth avatar ooloth commented on June 11, 2024 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.

anantoghosh avatar anantoghosh commented on June 11, 2024 1

@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.

anantoghosh avatar anantoghosh commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.