vue-cli-3tailwind-csscss-purge

How to configure PurgeCSS for vue-cli-3 projects with TailwindCSS? (including responsive classes)


I'm trying to deploy a vue-cli-3 project. I used TailwindCSS and created a vue.config.js file and it's working, but responsive classes are not being included. I searched about a regex, using a extractor in a webpack.config.js file but it didn't work. What should I do to have this working?

Here's my vue.config.js file

const PurgecssPlugin = require('purgecss-webpack-plugin')
const glob = require('glob-all')
const path = require('path')

module.exports = {
  configureWebpack: {
    // Merged into the final Webpack config
    plugins: [
      new PurgecssPlugin({
        paths: glob.sync([
          path.join(__dirname, './src/index.html'),
          path.join(__dirname, './**/*.vue'),
          path.join(__dirname, './src/**/*.js')
        ])
      })
    ]
  }
}

Where should I put the extractor array?


Solution

  • Update your configurations as following:

    const PurgecssPlugin = require('purgecss-webpack-plugin')
    const glob = require('glob-all')
    const path = require('path')
    
    module.exports = {
      configureWebpack: {
        // Merged into the final Webpack config
        plugins: [
          new PurgecssPlugin({
            paths: glob.sync([
              path.join(__dirname, './src/index.html'),
              path.join(__dirname, './src/**/*.vue'),
              path.join(__dirname, './src/**/*.js')
            ]),
            extractors: [
              {
                extractor: class TailwindExtractor {
                  static extract(content) {
                    return content.match(/[A-z0-9-_:\/]+/g) || [];
                  }
                },
                extensions: ['html', 'vue', 'js'],
              },
            ],
          })
        ]
      }
    }
    

    Also, I've noted that you used './**/*.vue' is that was intentionally or by mistake?