Remove unused CSS with PurgeCSS

We can tackle web performance in different ways, and one of them is removing all JS and CSS we don't use in our apps.

In the case of CSS, it's even more important when we use frameworks such as Bootstrap, Bulma or Tailwind, as well as when we're facing large or legacy applications.

PurgeCSS is a tool that removes unused CSS by applying string comparison. That has some advantages, but also some caveats, so please keep attention to the white-listing part later.

As an example, VueDose's website is built on Nuxt (static generated) using Tailwind, and it uses PurgeCSS to optimize the generated CSS.

If I disable PurgeCSS, you can see that the tailwind css is 485 KB:

While if I activate it, it goes down to 16 KB:

PurgeCSS configuration can be different in each project. It can be set as a webpack plugin or a postcss plugin.

In the case of VueDose, I'm using it as a postcss plugin. So I have a postcss.config.js file with this content:

const purgecss = require("@fullhuman/postcss-purgecss");

const plugins = [...];

if (process.env.NODE_ENV === "production") {
  plugins.push(
    purgecss({
      content: [
        "./layouts/**/*.vue",
        "./components/**/*.vue",
        "./pages/**/*.vue"
      ],
      whitelist: ["html", "body"],
      whitelistPatternsChildren: [/^token/, /^pre/, /^code/],
    })
  );
}

module.exports = { plugins };

Basically, all you need is to tell where to look for the matching classes using the content property.

Also, you'd like to whitelist some classes or tags to don't be removed. You'll need to do that at least in html and body, as well as any dynamic classes.

In my case, I use prismjs in order to highlight the code blocks, and it adds several token classes, as well as styles in the pre and code tags. In order to exclude them, I need to use the whitelistPatternsChildren property.

Tailwind, additionally, needs a custom extractor in order to work properly with PurgeCSS. All in all, the whole postcss.config.js file for VueDose has the following content:

const join = require("path").join;
const tailwindJS = join(__dirname, "tailwind.js");
const purgecss = require("@fullhuman/postcss-purgecss");

class TailwindExtractor {
  static extract(content) {
    return content.match(/[A-Za-z0-9-_:\/]+/g) || [];
  }
}

const plugins = [require("tailwindcss")(tailwindJS), require("autoprefixer")];

if (process.env.NODE_ENV === "production") {
  plugins.push(
    purgecss({
      content: [
        "./layouts/**/*.vue",
        "./components/**/*.vue",
        "./pages/**/*.vue"
      ],
      whitelist: ["html", "body"],
      whitelistPatternsChildren: [/^token/, /^pre/, /^code/],
      extractors: [
        {
          extractor: TailwindExtractor,
          extensions: ["html", "vue"]
        }
      ]
    })
  );
}

module.exports = {
  plugins
};

That's it for today!

Don't miss out anything about Vue, your favourite framework.

Subscribe to receive all the articles we publish in a concise format, perfect for busy devs.

Related Articles

Creating UI components based on a Design System in Vue.js

Don't you know the benefits of using a Design System? Not sure how to structure your Vue.js components in an app? Read the article and find it out.

Alex Jover Morales

Alex Jover Morales

Jul 21, 2020

How to structure a Vue.js app using Atomic Design and TailwindCSS

Unsure about how to organize your Vue.js components? Learn in this tutorial how to do it using Atomic Design methodology. Examples and demos included!

Alba Silvente

Alba Silvente

Jun 16, 2020

Theming using CSS Custom Properties in Vue.js Components

Learn in this article how to theme your applications and components by leveraging the power of CSS Custom Properties in your Vue.js components

César Alberca

César Alberca

Nov 5, 2019

Sponsors

VueDose is proudly supported by its sponsors. If you enjoy it, consider supporting it to ensure the project maintainability.

Silver
Learning Partner