question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It seems like it is impossible to safelist some TailwindCSS classes properly

See original GitHub issue

Hello!

It seems like it is impossible to safelist some TailwindCSS classes properly, for example: safelist: ['px-6', 'py-6', 'px-16', 'py-8', 'm-4', 'm-3', '-mt-32', 'pb-8', 'h-3', 'pt-12', 'h-32', '-mt-10', 'md:mt-4', 'ml-20', 'mt-16', 'mt-0', 'mt-8', 'ml-2', 'w-3/12', 'w-4/12', 'w-2/12', 'ml-8', 'ml-12'] It doesn’t safelist some classes like w-3/12, w-2/12 etc.

“tailwindcss”: “^2.0.1” “@fullhuman/postcss-purgecss”: “^3.0.0”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
pranav-jscommented, May 25, 2021

anyone who came after looking for fix, use

defaultExtractor: (content) => {
    // Capture as liberally as possible, including things like `h-(screen-1.5)`
    const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || [];

    // Capture classes within other delimiters like .block(class="w-1/2") in Pug
    const innerMatches = content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || [];

    return broadMatches.concat(innerMatches);
  },

inside your purgecss.config.js

1reaction
leviwheatcroftcommented, Aug 17, 2021

There’s actually some published extractors, as well as more detailed documentation.

In my own case I’m using nuxt / vue & pug, so the class attribute in templates can end up being a bit of a mess, like this:

  .menu(
    :class="[ { hidden: !active }, { flex: active }, 'flex-col', 'md:flex', 'md:flex-row', 'items-center' ]"
  )

I modified the published purgecss-from-pug extractor and I’m using this:

import lex from 'pug-lexer'

const purgeFromPug = (content) => {
  // see:
  // https://github.com/pugjs/pug-lexer/blob/master/test/cases/classes.expected.json
  // for an example of what the lexer return structure looks like
  // see:
  // https://purgecss.com/extractors.html#creating-an-extractor
  // for details about extractors, as well as some examples.
  const tokens = lex(content)
  const selectors = []
  for (const token of tokens) {
    if (token.type === 'class')
      selectors.push(...token.val.split(' '))
    else if (
      token.type === 'attribute' &&
      (
        token.name === 'class' ||
        token.name === ':class' ||
        token.name === 'id'
      )
    )
      selectors.push(...(token.val.match(/[\w-/:]+(?<!:)/g) || []))
  }
  return selectors
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

PurgeCSS doesn't safelist properly · Discussion #2961 - GitHub
PurgeCSS doesn't safelist properly. ... It seems like it is impossible to safelist some TailwindCSS classes properly, for example:
Read more >
Content Configuration - Tailwind CSS
The blocklist option only affects CSS that would be generated by Tailwind, not custom CSS you've authored yourself or are importing from another...
Read more >
Why are some Tailwind classes not having effect in JetStream?
There are plenty of possibilities which may cause this, but as a kind of a workaround you could use the safelist ...
Read more >
Upgrading to Tailwind CSS v3.0 - YouTube
In this video, I'll walk you through the most important steps when upgrading an existing project to Tailwind CSS v3.0.
Read more >
eslint-plugin-tailwindcss - npm
eslint-plugin-tailwindcss · Compatible with Tailwind CSS v3 and v2 · Make sure to use the correct version · We need you ❤️ ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found