Skip to content

Instantly share code, notes, and snippets.

@taylorbryant
Last active February 24, 2023 10:30
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save taylorbryant/91fc05b12472a88a8b6494f610647cd4 to your computer and use it in GitHub Desktop.
Save taylorbryant/91fc05b12472a88a8b6494f610647cd4 to your computer and use it in GitHub Desktop.
Use PurgeCSS with Tailwind & Gulp (Inspired by @andrewdelprete)
const atimport = require("postcss-import");
const { dest, src, task } = require("gulp");
const postcss = require("gulp-postcss");
const purgecss = require("@fullhuman/postcss-purgecss");
const tailwindcss = require("tailwindcss");
const TAILWIND_CONFIG = "./tailwind.config.js";
const SOURCE_STYLESHEET = "./src/style.css";
const DESTINATION_STYLESHEET = "./build/style.css";
task("css", () =>
src(SOURCE_STYLESHEET)
.pipe(
postcss([
atimport(),
tailwindcss(TAILWIND_CONFIG),
...(process.env.NODE_ENV === "production"
? [
purgecss({
content: ["**/*.html"],
defaultExtractor: content =>
content.match(/[\w-/:]+(?<!:)/g) || []
})
]
: [])
])
)
.pipe(dest(DESTINATION_STYLESHEET))
);
@idwebmx
Copy link

idwebmx commented Jul 17, 2019

You are awesome, thanks for your example!!!

@taylorbryant
Copy link
Author

No problem! Glad it helped! I've got a starter here if you're interested: https://github.com/taylorbryant/tailwind-jekyll

@CodeConvergence
Copy link

Thank you so much, this really helped me out with a WordPress + Tailwind setup!

@Vohra98
Copy link

Vohra98 commented Jan 21, 2020

Keep getting a Class constructor TailwindExtractor cannot be invoked without 'new' error

@taylorbryant
Copy link
Author

taylorbryant commented Jan 26, 2020

@Vohra98 PurgeCSS v2 doesn't use classes for extractors anymore.

Use this instead:

              purgecss({
                content: ["**/*.html"],
                defaultExtractor: content =>
                  content.match(/[\w-/:]+(?<!:)/g) || []
              })

@eMontielG
Copy link

@Vohra98 PurgeCSS v2 doesn't use classes for extractors anymore.

Use this instead:

              purgecss({
                content: ["**/*.html"],
                defaultExtractor: content =>
                  content.match(/[\w-/:]+(?<!:)/g) || []
              })

Thanks, been struggling with this for a while. All I found were examples from the previous v1 API.

@amitnaik999
Copy link

@Vohra98 PurgeCSS v2 doesn't use classes for extractors anymore.

Use this instead:

              purgecss({
                content: ["**/*.html"],
                defaultExtractor: content =>
                  content.match(/[\w-/:]+(?<!:)/g) || []
              })

Thank you

@lablnet
Copy link

lablnet commented Jul 20, 2021

Cool that's works thanks

@madruga8
Copy link

madruga8 commented Jul 29, 2022

the best setup on the internet ! not even the library makers could think simple like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment