Skip to content

Instantly share code, notes, and snippets.

@reinink
Created November 20, 2017 13:19
Show Gist options
  • Star 59 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save reinink/83058f9af402aa474010844f7f2b1c54 to your computer and use it in GitHub Desktop.
Save reinink/83058f9af402aa474010844f7f2b1c54 to your computer and use it in GitHub Desktop.
Using Purgecss with Tailwind and Laravel Mix
let cssImport = require('postcss-import')
let cssNext = require('postcss-cssnext')
let glob = require('glob-all')
let mix = require('laravel-mix')
let purgeCss = require('purgecss-webpack-plugin')
let tailwind = require('tailwindcss')
mix.js('resources/assets/js/app.js', 'public/js')
.postCss('resources/assets/css/app.css', 'public/css/app.css', [
cssImport(),
tailwind('tailwind.js'),
cssNext({ features: { autoprefixer: false }}),
])
.version()
if (mix.inProduction()) {
mix.webpackConfig({
plugins: [
new purgeCss({
paths: glob.sync([
path.join(__dirname, 'resources/views/**/*.blade.php'),
path.join(__dirname, 'resources/assets/js/**/*.vue')
]),
extractors: [
{
extractor: class {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g)
}
},
extensions: ['html', 'js', 'php', 'vue']
}
]
})
]
})
}
@ctf0
Copy link

ctf0 commented Nov 20, 2017

@wirelessed
Copy link

wirelessed commented Nov 21, 2017

Adding this code, whenever I run yarn run prod, there will be an strange unrelated error:
Module build failed: ReferenceError: Unknown plugin "transform-object-rest-spread" specified in "base" at 0, attempted to resolve relative to ".../.../..." Seems like the plugin has a conflict with babel-loader.
(Error doesn't appear when yarn run dev)

@fridzema
Copy link

fridzema commented Dec 6, 2017

For anyone who is running in the same error as @ctf0 this little terminal command will help you find the blank files.

find . -empty

@andrewdavidcostello
Copy link

In case anyone having the blank files issue comes across this.

It should be return content.match(/[A-z0-9-:\/]+/g) || []

@somecallmejosh
Copy link

Was looking at this issue: FullHuman/purgecss#30
Is it possible to use this extractor with Gulp? I have lots of classes like this:

cell-1\/2\@desktop {
   width: 50%;
}

In my Gulpfile I have this:

gulp.task('prod-sass', function () {
  // Delete old CSS files
  del(['static/css/**/*']);
  gulp.src('src/sass/**/*.scss')
    .pipe(sass({
      outputStyle : 'compressed'
    }))
   .pipe(
      purgecss({
        content: ['public/**/*.html'],
       extractors: {
        extractor: [/[A-z0-9-:\/]+/g)]
        }
      })
    )
    .pipe(gulp.dest('static/css'))
});

This doesn't seem to be working for me. Have I missed something?

@HSPDev
Copy link

HSPDev commented Aug 1, 2018

I got an annoying bug where Webpack tried to read in directories. Apparently I had one named "chart.js" and that got read in as it had .js in its end.

I fixed it using this option:

if(mix.inProduction())
{
    mix.webpackConfig({
        plugins: [
            new purgeCss({
                paths: glob.sync([
                    path.join(__dirname, 'resources/views/**/*.blade.php'),
                    path.join(__dirname, 'resources/assets/js/**/*.js'),
                    path.join(__dirname, 'resources/assets/theme/js/**/*.js')
                ], {
                    nodir: true,
                }),
                extractors: [
                    {
                        extractor: class {
                            static extract(content) {
                                return content.match(/[A-z0-9-:\/]+/g)
                            }
                        },
                        extensions: ['html', 'js', 'php', 'vue']
                    }
                ]
            })
        ]
    });

}

Notice the "nodir: true" option object...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment