Navigation Menu

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

for some reason keep getting error

92% recording/Users/novo/Public/www/_testing/five/node_modules/purgecss/lib/purgecss.js:1
(function (exports, require, module, __filename, __dirname) { "use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}function normalizeArray(t,e){for(var r=0,n=t.length-1;n>=0;n--){var i=t[n];"."===i?t.splice(n,1):".."===i?(t.splice(n,1),r++):r&&(t.splice(n,1),r--)}if(e)for(;r--;r)t.unshift("..");return t}function resolve(){for(var t="",e=!1,r=arguments.length-1;r>=-1&&!e;r--){var n=r>=0?arguments[r]:"/";if("string"!=typeof n)throw new TypeError("Arguments to path.resolve must be strings");n&&(t=n+"/"+t,e="/"===n.charAt(0))}return t=normalizeArray(filter(t.split("/"),function(t){return!!t}),!e).join("/"),(e?"/":"")+t||"."}function normalize(t){var e=isAbsolute(t),r="/"===substr(t,-1);return(t=normalizeArray(filter(t.split("/"),function(t){return!!t}),!e).join("/"))||e||(t="."),t&&r&&(t+="/"),(e?"/":"")+t}function isAbsolute(t){return"/"===t.charAt(0)}function join(){return normalize(filter(

Error: The extractor has failed to extract the selectors.
    at t.value (/Users/novo/Public/www/_testing/five/node_modules/purgecss/lib/purgecss.js:1:89301)
    at t.value (/Users/novo/Public/www/_testing/five/node_modules/purgecss/lib/purgecss.js:1:88744)
    at t.value (/Users/novo/Public/www/_testing/five/node_modules/purgecss/lib/purgecss.js:1:87941)
    at /Users/novo/Public/www/_testing/five/node_modules/purgecss-webpack-plugin/lib/purgecss-webpack-plugin.js:199:83
    at Array.forEach (<anonymous>)
    at /Users/novo/Public/www/_testing/five/node_modules/purgecss-webpack-plugin/lib/purgecss-webpack-plugin.js:182:27
    at Array.forEach (<anonymous>)
    at Compilation.<anonymous> (/Users/novo/Public/www/_testing/five/node_modules/purgecss-webpack-plugin/lib/purgecss-webpack-plugin.js:174:30)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:204:14)
    at Compilation.<anonymous> (/Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:275:11)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:204:14)
    at Compilation.<anonymous> (/Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:275:11)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:204:14)
    at Compilation.<anonymous> (/Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:275:11)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:204:14)
    at Compilation.<anonymous> (/Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:275:11)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:204:14)
    at Compilation.<anonymous> (/Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:275:11)
    at Compilation.applyPluginsAsyncSeries (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:206:13)
    at sealPart2 (/Users/novo/Public/www/_testing/five/node_modules/webpack/lib/Compilation.js:662:9)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:202:11)
    at Compilation.compilation.plugin (/Users/novo/Public/www/_testing/five/node_modules/webpack/lib/ProgressPlugin.js:111:6)
    at next (/Users/novo/Public/www/_testing/five/node_modules/tapable/lib/Tapable.js:204:14)
    at /Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:244:13
    at /Users/novo/Public/www/_testing/five/node_modules/async/dist/async.js:473:16
    at iteratorCallback (/Users/novo/Public/www/_testing/five/node_modules/async/dist/async.js:1050:13)
    at /Users/novo/Public/www/_testing/five/node_modules/async/dist/async.js:958:16
    at /Users/novo/Public/www/_testing/five/node_modules/extract-text-webpack-plugin/dist/index.js:227:15
    at /Users/novo/Public/www/_testing/five/node_modules/async/dist/async.js:473:16
    at iteratorCallback (/Users/novo/Public/www/_testing/five/node_modules/async/dist/async.js:1050:13)
error Command failed with exit code 1.

@reinink
Copy link
Author

reinink commented Nov 20, 2017

@ctf0 Hey! You're probably better posting this as an issue on the PurgeCSS repo: https://github.com/FullHuman/purgecss 👍

@snellingio
Copy link

@ctf0 ... Turns out, if you have a blank file included in the path.join, you will get that error. Spent the last few hours debugging this. Just go through your included directories and make sure you have no blank files.

@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