在进入下一个任务之前,让大嘴巴同步写入文件 [英] Making gulp write files synchronously before moving on to the next task
问题描述
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'...
然而,虽然反应任务已经完成,但它应该写入操作系统的文件还没有完成。我注意到,如果我在浏览器捆绑命令中加入睡眠语句,那么它就像预期的那样工作,但是这对我来说似乎有点不合理。
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.
这篇关于在进入下一个任务之前,让大嘴巴同步写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!