How to Setup Tailwind with PurgeCSS and PostCSS?
Tags: css, tailwindcss, webdev, beginners
Published: 2021-07-08 10:02:37

## Tailwind Installation

We have to install tailwind via npm or yarn:

```
npm init -y
npm install tailwindcss
```

## Create the Configuration File

We have to create a configuration for a tailwind to use:

```
npx tailwind init
```

This will create a file named tailwind.config.js in the root location of our project folder.

## PostCSS Config Tailwind

Tailwind needs PostCSS (PostCSS is a software development tool that uses JavaScript-based plugins to automate routine CSS operations) and autoprefixer (Autoprefixer will use the data based on current browser popularity and property support to apply prefixes for you) to work. So we need to apply that in the postcss.config.js file:

```js
module.exports = {
  plugins: [
    require('tailwindcss'),
    require('autoprefixer')
  ]
}
```

_**Note**_: If the postcss.config.js doesn’t exist, we have to create one.

We need to install PostCSS and autoprefixer from npm too:

```
npm install autoprefixer
npm install -g postcss-cli
```

## How to Create the Tailwind CSS File?

Now, we will create a CSS file `(style.css)` and add these lines at the top:

```css
@tailwind base;
@tailwind components;
@tailwind utilities;
```

## Create the build command

We need to specify a build command that will help us to generate the actual CSS file after compiling with PostCSS and autoprefixer. For that, we need to add the command to the `package.json` file at scripts like:

```js
"scripts": {
  "build:css": "postcss src/tailwind.css -o static/dist/tailwind.css"
}
```

## Build Tailwind

For building the final CSS file we need to run the command `npm run build` in the root of the project folder.

The resulting file is in `static/dist/tailwind.css`

### Automatically regenerate the CSS upon file changes

There is a great npm package that will compile our CSS in real-time without running the build command every time after edits. For that we need to install the watch using the command:

```
npm install watch
```

Then we need to edit the `package.json` file at scripts like:

```js
"scripts": {
  "build:css": "postcss src/tailwind.css -o static/dist/tailwind.css",
  "watch": "watch 'npm run build:css' ./layouts"
}
```

Now for running we simply need to execute the command `npm run watch` and it’s all good.

## Trim the File Size

If we check the final CSS file i.e after building, we can see that it’s huge in size. That large file is never appropriate for web pages. For that, we can actually trim the file to make it smaller than the original.

We need to install two more npm packages:

```
npm install cssnano
npm install @fullhuman/postcss-purgecss
```

### What is PurgeCSS?

PurgeCSS is a development tool used for removing the unused CSS in a Project. It is the default library to control the Tailwind CSS Bundle Sizes. It removes unused styles and optimizes CSS Build Sizes.

## How to Remove Unused Classes from Tailwind with PurgeCSS?

To remove unused CSS we use the following code. Then we add this to our PostCSS configuration file `postcss.config.js`:

```js
const purgecss = require('@fullhuman/postcss-purgecss')
const cssnano = require('cssnano')
module.exports = {
  plugins: [
    require('tailwindcss'),
    require('autoprefixer'),
    cssnano({
      preset: 'default'
    }),
    purgecss({
      content: ['./layouts/**/*.html', './src/**/*.vue', './src/**/*.jsx'],
      defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
    })
  ]
}
```

### In development, avoid too much processing

In development, we can use this just to add prefixes and remove comments. We need to edit the `postcss.config.js` like:

```js
const purgecss = require('@fullhuman/postcss-purgecss')
const cssnano = require('cssnano')
module.exports = {
  plugins: [
    require('tailwindcss'),
    process.env.NODE_ENV === 'production' ? require('autoprefixer') : null,
    process.env.NODE_ENV === 'production'
      ? cssnano({ preset: 'default' })
      : null,
    purgecss({
      content: ['./layouts/**/*.html', './src/**/*.vue', './src/**/*.jsx'],
      defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
    })
  ]
}
```