错误:任务完成回调调用次数过多

时间:2016-07-06 14:11:00

标签: javascript gulp

我正在将Typescript文件编译成从不同来源到不同目的地的Javascript。我需要知道何时完成所有编译以运行任务minifyJS

问题是我收到错误:Error: task completion callback called too many times。哪个是对的,但我想如何管理呢?

gulp.task('compileTS', function (done) {
    // Compile pages
    pump([
        gulp.src(paths.componentsTS),
        typescript({
            target: 'ES5'
        }),
        gulp.dest(paths.dest)
    ], done);

    // Compile services
    pump([
        gulp.src(paths.servicesTS),
        typescript({
            target: 'ES5'
        }),
        gulp.dest(paths.dest + 'Services/')
    ], done);

    // Compile tests
    pump([
        gulp.src(paths.testsTS),
        typescript({
            target: 'ES5'
        }),
        gulp.dest(paths.destTestsJS)
    ], done);
});

// This task needs to be called after 'compileTS' is done
gulp.task('minifyJS', ['compileTS'], function () {
    pump([
        gulp.src([paths.dest + paths.allJS]),
        uglify(),
        rename({
            suffix: '.min'
        }),
        gulp.dest(paths.dest)
    ]);
});

1 个答案:

答案 0 :(得分:2)

添加一些计数器(例如remains)以跟踪完整性:

gulp.task('compileTS', function (done) {
    var remains = 0;
    // Compile pages
    remains++;
    pump([
        gulp.src(paths.componentsTS),
        typescript({
            target: 'ES5'
        }),
        gulp.dest(paths.dest)
    ], completed);

    remains++;
    // Compile services
    pump([
        gulp.src(paths.servicesTS),
        typescript({
            target: 'ES5'
        }),
        gulp.dest(paths.dest + 'Services/')
    ], completed);

    remains++;
    // Compile tests
    pump([
        gulp.src(paths.testsTS),
        typescript({
            target: 'ES5'
        }),
        gulp.dest(paths.destTestsJS)
    ], completed);

    function completed () {
        if (--remains===0) {
            done(null, '');
        }
    }
});

处理我为你保留的错误。