PurgeCSS removing Tailwind font in next.js

2k views Asked by At

I have a next.js site I am building that uses a specific text as below,

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  theme: {
    extend: {
      fontFamily: {
        sans: ['SFMono-Regular', 'Menlo', ...defaultTheme.fontFamily.sans],
      },
      colors: {
        // indigo: '#7D00FF',
        blue: '#51B1E8',
        red: '#FF0E00',
      },
    },
  },
  plugins: [
    require('@tailwindcss/ui'),
  ]
}

For some reason the text style is being purged when it is deployed to Vercel. This is the purge css config.

module.exports = {
    plugins: [
      "postcss-import",
      "tailwindcss",
      "autoprefixer"
    ]
  };

  const purgecss = [
    "@fullhuman/postcss-purgecss",
    {
      content: [
        './pages/**/**/*.{js,jsx,ts,tsx}',
        './pages/**/*.{js,jsx,ts,tsx}',
        './pages/*.{js,jsx,ts,tsx}',

        './components/**/**/*.{js,jsx,ts,tsx}',
        './components/**/*.{js,jsx,ts,tsx}',
        './components/*.{js,jsx,ts,tsx}',
        ],
      defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
    }
  ];
  module.exports = {
    plugins: [
      "postcss-import",
      "tailwindcss",
      "autoprefixer",
      ...(process.env.NODE_ENV === "production" ? [purgecss] : [])
    ]
  };

What is going on?

Thanks in advance,

2

There are 2 answers

0
onewaveadrian On BEST ANSWER

I was able to solve this by adding html and body to the safelist in settings.

const purgecss = require('@fullhuman/postcss-purgecss')({
  // Specify the paths to all of the template files in your project
  content: [
    // './src/**/*.html',
    './pages/**/*.vue',
    './layouts/**/*.vue',
    './components/**/*.vue'
  ],
  safelist: ['html', 'body'],

  // Include any special characters you're using in this regular expression
  defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
})

Be careful which version of purgecss you have (check package.json): There was a change from whitelistPatterns to safelist which took me some time to find out

0
codejockie On

I have this set in my Vue project:

module.exports = {
  content: [
    "./src/**/*.vue",
  ],
  safelist: [
    "body",
    "html",
    "img",
    "a",
    "g-image",
    "g-image--lazy",
    "g-image--loaded",
    /-(leave|enter|appear)(|-(to|from|active))$/,
    /^(?!(|.*?:)cursor-move).+-move$/,
    /^router-link(|-exact)-active$/,
    /data-v-.*/,
  ],
  extractors: [
    {
      extractor: (content) => content.match(/[A-z0-9-:\\/]+/g),
      extensions: ["vue"],
    },
  ],
}

Depending on the version of PurgeCSS you are on, (mine was on: v3.1.3), the safelist is used for exclusion pattern, in older versions you might have to use whitelist instead.