Configuration 在'之后;html';Gulp中仍不存在任务文件index.html

Configuration 在'之后;html';Gulp中仍不存在任务文件index.html,configuration,gulp,task,Configuration,Gulp,Task,我运行简单配置: gulp.task('html', function() { gulp.src(config.paths.html) .pipe(gulp.dest(config.paths.dist)) .pipe(connect.reload()); }); gulp.task('connect', function() { server.listen(config.port); lrserver.listen(config.liv

我运行简单配置:

gulp.task('html', function() {
    gulp.src(config.paths.html)
        .pipe(gulp.dest(config.paths.dist))
        .pipe(connect.reload());
});

gulp.task('connect', function() {
    server.listen(config.port);
    lrserver.listen(config.livereloadport);
});

gulp.task('open', ['connect'], function() {
    gulp.src('dist/index.html')
        .pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'}));
});

gulp.task('default', ['html', 'open']);
但是,“打开”任务文件index.html上的第一次仍然不存在,只有第二次创建它并且任务“打开”将成功执行。我的配置有什么问题

我添加了一个控制台日志:

D:\Projects\demo>gulp
[11:01:13] Using gulpfile D:\Projects\demo\gulpfile.js
[11:01:13] Starting 'connect'...
[11:01:13] Finished 'connect' after 4.33 ms
[11:01:13] Starting 'html'...
[11:01:13] Finished 'html' after 9.03 ms
[11:01:13] Starting 'open'...
[11:01:13] Finished 'open' after 3.14 ms
[11:01:13] Starting 'default'...
[11:01:13] Finished 'default' after 9.82 μs

你们的任务相互依赖。您应该使“打开”依赖于“html”,以便ot将等待它完成:

gulp.task('html', function() {
    gulp.src(config.paths.html)
        .pipe(gulp.dest(config.paths.dist))
        .pipe(connect.reload());
});

gulp.task('connect', function() {
    server.listen(config.port);
    lrserver.listen(config.livereloadport);
});

gulp.task('open', ['connect', 'html'], function() {
    gulp.src('dist/index.html')
        .pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'}));
});

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

所以,我找到了解决办法。默认情况下,gulp中的所有任务都是并行运行的。因此,如果您想连续运行它们,您需要任务依赖关系,正如前面回答的那样:

gulp.task('open', ['connect', 'html'], function() {
    gulp.src('dist/index.html')
        .pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'}));
});
但除此之外,您还需要大口大口地了解上一项任务何时完成(添加返回):


这解决了我的问题。

不幸的是,它对我不起作用。请看控制台日志。在这两种情况下,顺序都是正确的。怎么了?请尝试在open中向src路径的开头添加./。另外,请尝试仅运行html,并确保该文件存在于dist/index.html/中。这样做没有帮助。我添加了console.log(fs.existsSync('./dist/index.html');对于“打开”任务,它第一次输出false,第二次输出true。因此,文件./dist/index.html仅在“默认”任务之后创建。感谢您和本文,我找到了下面给出的解决方案。再次感谢!
gulp.task('html', function() {
    return gulp.src(config.paths.html)
        .pipe(gulp.dest(config.paths.dist))
        .pipe(connect.reload());
});