Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gulp-if to conditionally compile less files?

Tags:

gulp

gulp-if

I can't get gulp-if to work correctly.

Without gulp-if it works fine:

gulp.task('css', function() {
    return gulp.src([
    //  'bower_components/bootstrap/dist/css/bootstrap.css',
        'app/stylesheets/main.less',
    ])
    .pipe(less({
        strictMath: true,
         strictUnits: true,
    }))
    .pipe(concat('all.css', {newLine: '\n'}))
    .pipe(prefixer('> 1%'))
    .pipe(minifyCss())
    .pipe(gulp.dest('public/css'));
});

But as soon as I add gulp-if into the mix, the gulpif pipe returns nothing when it returns to the main stream:

gulp.task('css', function() {
    return gulp.src([
        'bower_components/bootstrap/dist/css/bootstrap.css',
        'app/stylesheets/main.less',
    ])

        .pipe(gulpif(/\.less$/,less({
            strictMath: true,
            strictUnits: true,
        })))
        .pipe(concat('all.css', {newLine: '\n'}))
        .pipe(prefixer('> 1%'))
        .pipe(minifyCss())
        .pipe(gulp.dest('public/css'));
});

Why is that? What am I doing wrong? If I add some logs into the gulp-if source code I can see that the condition is passing.

like image 328
mpen Avatar asked Apr 16 '14 05:04

mpen


2 Answers

I think you may want to use gulp-filter instead: https://www.npmjs.org/package/gulp-filter

gulp.task('css', function() {
    var lessFilter = gulpFilter('**/*.less', {restore: true})
    return gulp.src([
        'bower_components/bootstrap/dist/css/bootstrap.css',
        'app/stylesheets/main.less',
    ])

    .pipe(lessFilter)
    .pipe(less({
        strictMath: true,
        strictUnits: true,
    }))
    .pipe(lessFilter.restore)

    .pipe(concat('all.css', {newLine: '\n'}))
    .pipe(prefixer('> 1%'))
    .pipe(minifyCss())
    .pipe(gulp.dest('public/css'));
});

This will create a filtered stream and the restore() will restore the stream. gulpIf is made to conditionally process a whole stream and not just matching files.

gulp.src('src/**/*.js', function () {
    .pipe(gulpIf(isProduction, concat('all.js')))
    .pipe(gulp.dest('dest/')
});
like image 113
Nicholas Boll Avatar answered Oct 23 '22 18:10

Nicholas Boll


This sounds like a bug in gulp-if. Try upgrading gulp-if to the latest version, and if that doesn't work, post an issue on https://github.com/robrich/gulp-if/issues.

like image 3
robrich Avatar answered Oct 23 '22 20:10

robrich