当前位置: 代码迷 >> JavaScript >> Gulp同步任务问题
  详细解决方案

Gulp同步任务问题

热度:64   发布时间:2023-06-13 12:27:53.0

我正在尝试收集调试所需的3个任务。当然,由于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'));

在gulp中你可以实际设置依赖任务。 试试这个:

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

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

我认为你没有定义'debugAll'任务权利。 试试这样:

gulp.task('debugAll', function () { 
    runSequence('debug', 'rename', 'final');
});

而且你还需要为这些任务返回流,只需在gulp.src前面为每个任务添加'return':debug,rename,final。 以下是“调试”任务的示例:

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

这两个项目都在文档中提到: :