PurgeCSS and Tailwind CSS, how to preserve responsive classes using the Command Line Interface?

Tristan Bains

I have the "full" 3.9 MB Tailwind CSS file and successfully applied PurgeCSS to reduce it to 9 kB. But it also purged all responsive classes like md:px-6, they don't show up in my purged version.

Note: this question is for using the command line interface (CLI)

This is what I did:

purgecss --css ~/Desktop/Projects/Flask/Project1/build/static/css/main.css --content ~/Desktop/Projects/Flask//Project1/build/**/*.html --output ~/Desktop/Projects/Flask/Project2/static/css/main.css

I chose to create the output file in a different folder (Project2) so that I could check on the input vs output.

One thing I tried is to add --safelist [/md/], but didn't help. In fact the safelist didn't seem to be used at all...

(I use CLI since it is part of a bigger Python Flask project)

Zsolt Meszaros

PurgeCSS relies on extractors to get the list of selectors used in a file. It provides a default extractor that is working fine with a wide variety of file types, but it can be limited and does not fit every CSS framework out there.

The default extractor considers every word of a file as a selector but it doesn't consider special characters like the colon (:) which is heavily used in Tailwind CSS.

So, by default, PurgeCSS removes responsive (md:px-6), hover (hover:bg-gray-500), etc. classes. To avoid this, Tailwind has its own extractor. You could use this (or your very own) extractor but the PurgeCSS CLI has limited options and it's missing a defaultExtractor option.

Luckily, it accepts a config file option, so if you create your own purgecss.config.js file and add a default extractor in there, it will preserve these classes too. You can also add your other options to this file.

I used to use this simple extractor which will work for you too:

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

Your config file will look like this:

// purgecss.config.js
module.exports = {
  content: ['build/**/*.html'],
  css: ['build/static/css/main.css'],
  defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
  output: 'static/css/main.css',
};

And you can use the following command to run PurgeCSS with the above config:

purgecss --config ./purgecss.config.js

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to configure PurgeCSS for vue-cli-3 projects with TailwindCSS? (including responsive classes)

Using Tailwind opacity classes with CSS variables

How in Tailwind CSS to use classes with arbitrary values

responsive divider between two column using tailwind css

Tailwind CSS - Responsive breakpoints as components

tailwind css responsive grid issue

Responsive text in label with tailwind css

Tailwind.css — How to implement the last-child using Tailwind?

Tailwind responsive is not working using width

How to preserve whitespace but ignore line breaks in CSS?

"Semicolon or block is expected" error when using tailwind responsive classes in svelte-kit style tags

How to show data in a table by using psql command line interface?

Flink: How to cancel the correct job using the command line interface?

How to create a signed APK file using Cordova command line interface?

How to get date and time using command line interface?

How to change the nexus repository url using command line interface?

2 line button renders incorrect using Tailwind CSS

How to fill in a Command Line Interface?

Tailwind CSS is not outputting classes for the background

How to make it responsive using css

Tailwind CSS responsive behavior on nextjs app

Preserve line breaks with extra text in a dialog using the 'dialog' command

How to preserve line breaks when storing a command output to a variable in bash?

How does shell preserve command file input line boundary?

Creating a Calculator using command line interface and malloc,

Using click to create a complex command line interface

Using sqlite CLI (Command Line Interface) with dates

Number of intergers in a file using Command Line Interface

Using command line Interface app as library