【问题标题】:How to exit a task with gulp-if如何使用 gulp-if 退出任务
【发布时间】:2019-12-03 21:39:49
【问题描述】:

我有一个处理图像的 gulp 4 任务。首先,它会缩小它们,如果满足条件,则应将 SVG 转换为 PHP 部分。如果不满足,我该如何实现这样的条件并停止任务。

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

【问题讨论】:

  • 你要结束任务还是一起退出 Gulp?
  • 我只想结束任务

标签: gulp gulp-4


【解决方案1】:

gulp-fail - 看起来它是为 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?")));
})

【讨论】:

    【解决方案2】:

    最后我的解决方案是简单地将 dest() 也包含在 if 中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2017-02-06
      • 2017-05-23
      • 1970-01-01
      • 2016-09-25
      相关资源
      最近更新 更多