在继续下一个任务之前使 gulp 同步写入文件

2022-01-12 00:00:00 node.js javascript gulp

gulpfile.js

gulpfile.js

gulp.task('browser-bundle', ['react'], function() {
...

});


gulp.task('react', function(){
  gulp.src(options.JSX_SOURCE)
      .pipe(react())
      .pipe(gulp.dest(options.JSX_DEST))
});

如您所见,我有浏览器捆绑任务,具体取决于反应任务.我相信这可以按预期工作,因为在输出中我看到了:

As you can see I have the browser-bundle task depending on the react task. I believe this works as expected because in the output I see this:

[gulp] Running 'react'...
[gulp] Finished 'react' in 3.43 ms
[gulp] Running 'browser-bundle'...

然而,虽然 react 任务已经完成,但它应该写入操作系统的文件还没有完全到位.我注意到,如果我在浏览器捆绑命令中添加了一个 sleep 语句,那么它会按预期工作,但这对我来说似乎有点 hacky.

However, although the react task is finished, the files its supposed to write to the operating system are not quite there yet. I've notice that if I put a sleep statement in the browser bundle command then it works as expected, however this seems a little hacky to me.

如果我希望在文件(来自 gulp.dest)被同步写入磁盘之前不认为反应任务完成,我该怎么做?

If I want the react task to not be considered finished until the files (from gulp.dest) have been synchronously written to disk how would I do that?

推荐答案

你需要一个return语句:

You need a return statement:

gulp.task('react', function(){
    return gulp.src(options.JSX_SOURCE)
        .pipe(react())
        .pipe(gulp.dest(options.JSX_DEST))
});

这样,我所有的写操作都在下一个任务处理之前完成.

With this all my write operations are done before the next task processed.

相关文章