在这样的片段中:

gulp.task "coffee", ->
    gulp.src("src/server/**/*.coffee")
        .pipe(coffee {bare: true}).on("error",gutil.log)
        .pipe(gulp.dest "bin")

gulp.task "clean",->
    gulp.src("bin", {read:false})
        .pipe clean
            force:true

gulp.task 'develop',['clean','coffee'], ->
    console.log "run something else"

在开发任务中,我想要干净地运行,在它完成后,运行咖啡,当它完成时,运行其他东西。但是我想不出来。这个零件坏了。请建议。


当前回答

根据Gulp的文档:

您的任务是否在依赖项完成之前运行?确保你的依赖任务正确使用异步运行提示:接受回调或返回承诺或事件流。

同步运行你的任务序列:

返回事件流(例如gulp.src)给gulp。通知任务 流何时结束的任务。 在gulp.task的第二个参数中声明任务依赖性。

参见修订后的代码:

gulp.task "coffee", ->
    return gulp.src("src/server/**/*.coffee")
        .pipe(coffee {bare: true}).on("error",gutil.log)
        .pipe(gulp.dest "bin")

gulp.task "clean", ['coffee'], ->
      return gulp.src("bin", {read:false})
        .pipe clean
            force:true

gulp.task 'develop',['clean','coffee'], ->
    console.log "run something else"

其他回答

这个问题的唯一好的解决方案可以在gulp文档中找到:

var gulp = require('gulp');

// takes in a callback so the engine knows when it'll be done
gulp.task('one', function(cb) {
  // do stuff -- async or otherwise
  cb(err); // if err is not null and not undefined, the orchestration will stop, and 'two' will not run
});

// identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
  // task 'one' is done now
});

gulp.task('default', ['one', 'two']);
// alternatively: gulp.task('default', ['two']);

我使用生成器-gulp-webapp Yeoman生成器生成了一个node/gulp应用程序。它是这样处理“干净的难题”的(翻译成问题中提到的原始任务):

gulp.task('develop', ['clean'], function () {
  gulp.start('coffee');
});

对我来说,它不是在连接后运行minify任务,因为它期望连接的输入,而且它没有生成一些时间。

我尝试按执行顺序添加到默认任务,但没有工作。在为每个任务添加一个返回值并在gulp.start()中得到如下所示的缩小后,它就工作了。

/**
* Concatenate JavaScripts
*/
gulp.task('concat-js', function(){
    return gulp.src([
        'js/jquery.js',
        'js/jquery-ui.js',
        'js/bootstrap.js',
        'js/jquery.onepage-scroll.js',
        'js/script.js'])
    .pipe(maps.init())
    .pipe(concat('ux.js'))
    .pipe(maps.write('./'))
    .pipe(gulp.dest('dist/js'));
});

/**
* Minify JavaScript
*/
gulp.task('minify-js', function(){
    return gulp.src('dist/js/ux.js')
    .pipe(uglify())
    .pipe(rename('ux.min.js'))
    .pipe(gulp.dest('dist/js'));
});

gulp.task('concat', ['concat-js'], function(){
   gulp.start('minify-js');
});

gulp.task('default',['concat']); 

源http://schickling.me/synchronous-tasks-gulp/

我一直在寻找这个答案。现在我在gulp的官方文档里找到了。

如果你想在最后一个任务完成时执行gulp任务,你必须返回一个流:

饮而尽。任务('wiredep', ['dev-jade'],函数(){ Var stream = gulp.src(路径。输出+ '*.html') .pipe ($ .wiredep ()) .pipe (gulp.dest (paths.output)); 返回流;//当此任务完成时执行下一个任务 }); //首先执行并完成wiredep任务 饮而尽。任务('prod-jade', ['wiredep'],函数(){ gulp.src(路径。输出+ '**/*.html') .pipe ($ .minifyHtml ()) .pipe (gulp.dest (paths.output)); });

Gulp和Node使用承诺。

所以你可以这样做:

// ... require gulp, del, etc

function cleanTask() {
  return del('./dist/');
}

function bundleVendorsTask() {
  return gulp.src([...])
    .pipe(...)
    .pipe(gulp.dest('...'));
}

function bundleAppTask() {
  return gulp.src([...])
    .pipe(...)
    .pipe(gulp.dest('...'));
}

function tarTask() {
  return gulp.src([...])
    .pipe(...)
    .pipe(gulp.dest('...'));
}

gulp.task('deploy', function deployTask() {
  // 1. Run the clean task
  cleanTask().then(function () {
    // 2. Clean is complete. Now run two tasks in parallel
    Promise.all([
      bundleVendorsTask(),
      bundleAppTask()
    ]).then(function () {
      // 3. Two tasks are complete, now run the final task.
      tarTask();
    });
  });
});

如果返回gulp流,则可以使用then()方法添加回调。或者,您可以使用Node的本机Promise创建自己的Promise。在这里,我使用Promise.all()来获得一个回调,当所有promise都解决时触发。