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']
}
]
})
]
})
}
@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