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

Making gulp write files synchronously before moving on to the next task(在继续下一个任务之前使 gulp 同步写入文件)
本文介绍了在继续下一个任务之前使 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.

这篇关于在继续下一个任务之前使 gulp 同步写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)