gulpgulp-4

How to exit a task with gulp-if


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));
}

Solution

  • 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)));
    }