Skip to content

Instantly share code, notes, and snippets.

@thinkverse
Last active July 12, 2023 12:12
Show Gist options
  • Save thinkverse/02e4efb8fa11487e0d5e27d5a50e00d5 to your computer and use it in GitHub Desktop.
Save thinkverse/02e4efb8fa11487e0d5e27d5a50e00d5 to your computer and use it in GitHub Desktop.
Useful TailwindCSS config that saves paddings and margins upon build
// https://v1.tailwindcss.com/docs/controlling-file-size#purge-css-options
module.exports = {
future: {
removeDeprecatedGapUtilities: true,
purgeLayersByDefault: true,
},
purge: {
content: ['./public/**/*.html'],
options: {
whitelistPatterns: [
/^\-?m(\w?)-/,
/^p(\w?)-/,
/^text-/,
/^bg-/,
]
}
},
theme: {
extend: {},
},
variants: {},
plugins: [],
}
// https://v2.tailwindcss.com/docs/optimizing-for-production#safelisting-specific-classes
module.exports = {
purge: {
content: ['./public/**/*.html'],
options: {
safelist: [
/^\-?m(\w?)-/,
/^p(\w?)-/,
/^text-/,
/^bg-/,
]
}
},
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
// https://tailwindcss.com/docs/content-configuration#safelisting-classes
module.exports = {
content: ['./public/**/*.html'],
safelist: [
'text-2xl',
'text-3xl',
{ pattern: /^\-?m(\w?)-/ },
{ pattern: /^p(\w?)-/ },
{ pattern: /^text-/ },
{ pattern: /^bg-/ },
],
theme: {
extend: {},
},
plugins: [],
}
@thinkverse
Copy link
Author

thinkverse commented Nov 11, 2020

If this is written correctly - my knowledge of RegEx is minimal, this config will save all p-, m-, bg- and, text- classes.
Excluding the variants like hover:text-gray-200, lg:mx-12, and so on.

@thinkverse
Copy link
Author

Didn't have time to test this yesterday. But did today, and, yes this does indeed save all p-, m-, bg- and, text- classes upon build. 🙂
Adds around 40KB extra to the finished build. That does include all the margins, padding, background, and text classes you use in your project by default anyway so in the end it's not that much overhead IMHO.

@thinkverse
Copy link
Author

TailwindCSS v2 will be using PurgeCSS v3 and won't have the whitelistPatterns option, instead, it will use the safelist key so I added a version for that as well. 👍

@rahmanramsi
Copy link

Thanks it really help me

@kuzdogan
Copy link

kuzdogan commented May 4, 2021

Thanks really useful. Just as fyi I guess you don't need to escape the initial - in:
/^\-?m(\w?)-/, so can be
/^-?m(\w?)-/

@k1sul1
Copy link

k1sul1 commented Apr 6, 2022

FYI the syntax seems to have changed in v3.

This is what worked for me:

module.exports = {
  content: ["./src/**/*.{js,ts,jsx,tsx}", "./**/*.php"],
  
  // Save padding, margin, text and bg classes from purging
  safelist: [
    { pattern: /^\-?m(\w?)-/ },
    { pattern: /^p(\w?)-/ },
    { pattern: /^text-/ },
    { pattern: /^bg-/ },
  ],

Quite necessary feature to have when I want to allow all 0-96 margin classes for my editors.

@thinkverse
Copy link
Author

FYI the syntax seems to have changed in v3.

Quite right, it did. I had forgotten about this gist so haven't updated it. Thanks for reminding me about it, added your code to a new file. 👍 With a mix of what's in the documentation.

@k1sul1
Copy link

k1sul1 commented Apr 6, 2022

This page was the first hit for whatever I googled. Something along the lines of "tailwind don't purge padding classes". It did a great job at pointing me in the right direction :)

@hari-d
Copy link

hari-d commented Jun 13, 2022

how to write a pattern for
p-[50px] or m-[50px]
and for all tailwind classes which can be written dynamic using square brackets like the above one.

@thinkverse
Copy link
Author

@hari-d haven't used safelist myself for arbitrary values, but this thread1 on Tailwind's discussions page could be useful for you.

Footnotes

  1. Safelist pattern for arbitrary values - https://github.com/tailwindlabs/tailwindcss/discussions/7908

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