how to pass settings to postcss modules in gulp, especially to uncss?

371 views Asked by At

I am not able to pass settings to the uncss module in case I want to call it within postcss within a gulp script.

Here is my gulp task:

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var cssvars = require('postcss-simple-vars');
var nested = require('postcss-nested');
var cssImport = require('postcss-import');
var mixins = require('postcss-mixins');
var uncss = require('postcss-uncss');
var autoprefixer = require('autoprefixer');

var autoprefixeroptions = {
        "browsers": [
            "last 3 version",
            "> 1%"
        ]
        }

var uncssoptions =  {
        html: ['dest/index.html'],
        ignore: ['.text-right', '.text-left', '.affix', '.navbar-default.affix',
            /\w\.in/,
            '.fade',
            '.collapse',
            '.collapsing',
            /(#|\.)navbar(\-[a-zA-Z]+)?/,
            /(#|\.)dropdown(\-[a-zA-Z]+)?/,
            /(#|\.)(open)/,
            '.modal',
            '.modal.fade.in',
            '.modal-dialog',
            '.modal-document',
            '.modal-scrollbar-measure',
            '.modal-backdrop.fade',
            '.modal-backdrop.in',
            '.modal.fade.modal-dialog',
            '.modal.in.modal-dialog',
            '.modal-open',
            '.in',
            '.modal-backdrop'
        ]
        };


gulp.task('styles', function() {
    var plugins = [
        cssImport,
        mixins,
        cssvars,
        nested,
        uncss(uncssoptions),
        autoprefixer(autoprefixeroptions)
        ];
    return gulp.src('./src/css/*.css')
        .pipe(postcss(plugins))
        .pipe(gulp.dest('./dest/css'));

})

When I try this, I get at runtime (when I change something in a css file) the folowing error message:


[Browsersync] Serving files from: dest
[12:20:39] Starting 'styles'...
[12:20:39] 'styles' errored after 295 μs
[12:20:39] TypeError: uncss is not a function
    at Gulp.<anonymous> (C:\Projects\test\gulp\tasks\styles.js:50:3)
    at module.exports (C:\Projects\test\node_modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\Projects\test\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (C:\Projects\test\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (C:\Projects\test\node_modules\orchestrator\index.js:134:8)
    at C:\Projects\test\gulp\tasks\watch.js:19:10
    at write (C:\Projects\test\node_modules\gulp-watch\index.js:147:3)
    at C:\Projects\test\node_modules\gulp-watch\index.js:130:5

So, how else fo i setup uncss in a gulp file, when i call it as a postcss module?

Thank you

Kai

1

There are 1 answers

1
Kai On

Ok, I solved it myself. I forgot to use uncss in the package.json file along with postcss-uncss. Now it works.

I hope you like that contribution. In all my googling, i was not able to find this answer in the web.

BTW, this sample shows how to use uncss on Bootstrap.