TailwindCSS / PurgeCSS extractor string removing some classes
Asked Answered
D

2

6

fairly new to Tailwind and PostCSS/PurgeCSS, so hoping this is a fairly simple fix.

In my tailwind.config.js, I am extending some of the spacing values, including adding a 0.5 value to align with the default Tailwind spacing scale. My file looks like this (extract):

module.exports = {
    important: false,
    theme: {
        spacing: {
            '0.5': '0.125rem',
        },
    },
}

I'm then using PostCSS to compile my CSS, which looks as follows, as you can see I'm using a bunch of plugins which work great:

module.exports = {
    parser: 'postcss-scss',
    plugins: [
        require('postcss-import'),
        require('postcss-nested'),
        require('postcss-responsive-type'),
        require('tailwindcss'),
        require('autoprefixer'),
        require('cssnano'),
    ]
}

Up to this point, all working great! However, I want to purge the css to remove all of the unused utility classes that Tailwind creates. This effects my postcss file as follows:

module.exports = {
    parser: 'postcss-scss',
    plugins: [
        require('postcss-import'),
        require('postcss-nested'),
        require('postcss-responsive-type'),
        require('tailwindcss'),
        require('autoprefixer'),
        require('@fullhuman/postcss-purgecss')({
            content: [
                './*.php',
                './**/*.php',
            ],
            defaultExtractor: content => content.match(/[\w-:/]+(?<!:)/g) || []
        }),
        require('cssnano'),
    ]
}

This is the point at which I lose some styles, specifically the specially configued Tailwind ones such as .h-0.5.

I siuspect the issue is within the defaultExtractor line?

defaultExtractor: content => content.match(/[\w-:/]+(?<!:)/g) || []

Anyone able to lend a hand? Thanks

Donnell answered 5/3, 2020 at 18:15 Comment(0)
D
5

Solved! It was, as expected, very simple. I was simply missing the '.' for the defaultExtractor:

defaultExtractor: content => content.match(/[\w-:./]+(?<!:)/g) || []
Donnell answered 5/3, 2020 at 19:49 Comment(4)
yet "." Is not being excluded? defaultExtractor: content => content.match(/[\w-:./]+(?<!:)/g) || []Uziel
I've used @soria's comment and @dupflo's answer to do an edit here, the author clearly intended to include the . but it was omitted, this is now fixedPrerecord
(Repost) Tailwind now also allows classes like: w-[100px] or bg-[#A1B2C3]. I've added the allowance of: [, ], % and # into the regex to support those too: /[\w\-:.\/\[#%\]]+(?<!:)/gMares
This doesn't work with tailwindcss 3.2; any updates on this one?Neighborly
G
2
defaultExtractor: (content) => content.match(/[\w-:./]+(?<!:)/g) || [],

Fixed it

Gamo answered 3/6, 2021 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.