0

I have a gulp 4 task that processes images. First it minifies them and if a condition is met, SVGs should be turned into PHP partials. How can I implement such a condition and stop the task, if it's not met.

function processImages() {
    return src(src)
        .pipe(
            imagemin([
                imagemin.gifsicle({
                    interlaced: true,
                }),
                imagemin.jpegtran({
                    progressive: true,
                }),
                imagemin.optipng({
                    optimizationLevel: 3,
                }),
                imagemin.svgo({
                    plugins: [
                        {
                            removeViewBox: false,
                        },
                        {
                            cleanupIDs: false,
                        },
                    ],
                }),
            ]),
        )
        .pipe(dest(dest))
        // STOP AFTER THIS STEP IF CONDITION IS NOT MET
        .pipe(gulpif(!shouldCreatePartials(), exit()))
        .pipe(filter(file => /svg$/.test(file.path)))
        .pipe(
            rename({
                extname: '.php',
            }),
        )
        .pipe(dest(partialsDest));
}
David M
  • 99
  • 6

2 Answers2

0

See gulp-fail - looks like it was made to work with gulp-if.

var fail   = require('gulp-fail');
var gulpIf = require('gulp-if');

var condition = true;


// Will fail with the given message shown as the reason.
gulp.task('mytask', function() {
  return gulp.src('**/*.js')
             .pipe(gulpIf(condition, fail("It failed cause I told it to! ...Hooray?")));
})
Mark
  • 143,421
  • 24
  • 428
  • 436
0

In the end my solution was to simply wrap dest() in an if as well.

function processImages() {
    return src(src)
        .pipe(imagemin(),)
        .pipe(dest(dest))
        .pipe(gulpif(shouldCreatePartials(), filter(file => /svg$/.test(file.path))))
        .pipe(gulpif(shouldCreatePartials(), rename({
           extname: '.php',
         })))
        .pipe(gulpif(shouldCreatePartials(), dest(partialsDest)));
}
David M
  • 99
  • 6