如何强制 gulp 调用同步运行?

2022-01-12 00:00:00 node.js javascript gulp

我希望下面的 gulp 调用一个接一个地同步运行.但他们不服从命令.

I want the gulp calls below to run synchronously, one after the other. But they do not follow an order.

run-sequence 节点模块在这里没有帮助,因为我不想运行 gulp串联任务(即,它的语法类似于 gulp.task("mytask", ["foo", "bar", "baz"] 等),而是串联 gulp 调用",如下所示.

The run-sequence node module doesn't help here, as I'm not trying to run gulp tasks in series (i.e. it has syntax similar to gulp.task("mytask", ["foo", "bar", "baz"] etc.), but rather gulp "calls" in series, as you see below.

gulp.task("dostuff", function (callback) {

  gulp
    .src("...")
    .pipe(gulp.dest("...");

  gulp
    .src("...")
    .pipe(gulp.dest("...");

  gulp
    .src("...")
    .pipe(gulp.dest("...");

  callback();
});

如何让它们一个接一个地运行?

How do I make them run one after the other?

推荐答案

你可以使用async 作为您的呼叫的控制流程,让他们只完成一项任务,同时避免您获得金字塔效应".所以这样的事情应该对你的用例有好处:

You can use async as a control flow for your calls to get them in only one task, also avoiding you to get a "pyramid effect". So something like this should be good for your use-case:

var async = require('async');

gulp.task('yeah', function (cb) {
  async.series([
    function (next) {
      gulp.src('...')
        .pipe(gulp.dest('...')
        .on('end', next);
    },
    function (next) {
      gulp.src('...')
        .pipe(gulp.dest('...')
        .on('end', next);
    },
    function (next) {
      gulp.src('...')
        .pipe(gulp.dest('...')
        .on('end', next);
    }
  ], cb);
});

这也将允许您进行一些错误处理并更好地定位发生问题的位置.

That will also allow you to have some error handling and target better where a problem occured.

相关文章