Gulp同步任务问题

3

我正在尝试将3个调试任务汇集到1个中来完成。由于gulp的本质是异步的,所以我遇到了一些问题。所以我搜索并找到了一个解决方案,使用run-sequence模块来解决这个问题。我尝试了下面的代码,但它似乎没有按预期工作。它无法同步。

以下是我的尝试,请问大家有什么想法吗?我不想运行所有这三个命令才能完成所有任务。我该怎么办?

var gulp = require('gulp'),
    useref = require('gulp-useref'),
    gulpif = require('gulp-if'),
    debug = require('gulp-debug'),
    rename = require("gulp-rename"),    
    replace = require('gulp-replace'),
    runSequence = require('run-sequence'),
    path = '../dotNet/VolleyManagement.UI';  

gulp.task('debug', function () {
    gulp.src('client/*.html')
        .pipe(debug())
        .pipe(gulp.dest(path + '/Areas/WebAPI/Views/Shared'));
});

gulp.task('rename', function () {    
    gulp.src(path + '/Areas/WebAPI/Views/Shared/index.html')
        .pipe(rename('/Areas/WebAPI/Views/Shared/_Layout.cshtml'))
        .pipe(gulp.dest(path));        

    gulp.src(path + '/Areas/WebAPI/Views/Shared/index.html', {read: false})
        .pipe(clean({force: true})); 
});

gulp.task('final', function(){
    gulp.src([path + '/Areas/WebAPI/Views/Shared/_Layout.cshtml'])
        .pipe(replace('href="', 'href="~/Content'))
        .pipe(replace('src="', 'src="~/Scripts'))
        .pipe(gulp.dest(path + '/Areas/WebAPI/Views/Shared/'));
}); 

gulp.task('debugAll', runSequence('debug', 'rename', 'final'));
2个回答

3

在gulp中,您可以设置依赖任务。尝试这样做:

gulp.task('debug', function () {
    //run debug task
});

gulp.task('rename',['debug'], function () {    
    //run rename once debug is done
});

1
我尝试过了,但问题在于它也是异步运行的... 'debug' 任务在 'rename' 之后完成。 - Ivan

1
我认为你没有正确定义“debugAll”任务。尝试像这样:
gulp.task('debugAll', function () { 
    runSequence('debug', 'rename', 'final');
});

同时,你需要为这些任务返回流,只需在每个任务的gulp.src前添加'return'即可:debug、rename、final。以下是'debug'任务的示例:

gulp.task('debug', function () {
    return gulp.src('client/*.html')
        .pipe(debug())
        .pipe(gulp.dest(path + '/Areas/WebAPI/Views/Shared'));
});

两个项目都在文档中提到了: https://www.npmjs.com/package/run-sequence


是的,我的错。但我现在尝试了正确的语法,无论如何也不能一个接一个地工作... - Ivan
1
一开始没有注意到,但实际上你需要返回这些任务的流。只需在每个任务 - debug、rename、final 的 gulp.src 前面添加 'return' 即可。@ramesh 上面的答案也存在同样的问题 - 你没有返回流。文档中也提到了这一点:https://www.npmjs.com/package/run-sequence - Liviu Costea
这正是我所错过的!谢谢你指出来,现在一切都正常工作了 :) 附言:你能编辑一下你的回答吗?加上上面的信息,这样我就可以把你的回答标记为已接受。 - Ivan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接