A Zero-config Purgecss Wrapper for Laravel Mix


January 26th, 2018

Sebastian De Deyne over at Spatie is working on a Purgecss package for Laravel Mix. The package provides zero-config Purgecss for your average Laravel project:

let mix = require('laravel-mix');

// ...

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css')

All HTML, JS, PHP, and Vue files in the app and resource folders get scanned for selectors, and Purgecss is only enabled in production.

Most project shouldn’t need it, but if you need to go beyond the basics, you can configure Purgecss options and three options provided by the laravel-mix-purgecss package:

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css')
   .purgeCss({ /* My options */ });

Check the readme for more information on how to configure Purgecss and the additional custom options this package provides Laravel Mix.

If you are not familiar with Purgecss:

Purgecss is a tool to remove unused CSS. It can be used as part of your development workflow. Purgecss comes with a JavaScript API, a CLI, and plugins for popular build tools.

This package gives you Purgecss for free on your Laravel projects, so you can install it and benefit from smaller CSS files in production. Boom!

Check the package out at spatie/laravel-mix-purgecss on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.