您如何“结束"运行大口吃任务,但仅在当前任务结束时? [英] How do you run a gulp "on end" task but only at the end of the current task?
问题描述
我正在使用Gulp收集前题(通过gulp-front-matter插件),然后在对其进行汇总之后,将其保存到另一个文件中.在其他数据中,我节省了很多CSS.这是我为compileCSS
任务准备的东西:
I'm using Gulp to collect front-matter (via gulp-front-matter plugin) and then, after aggregating it, I'm saving it into another file. Among other data, I'm saving a bunch of CSS. Here's what I have for my compileCSS
task:
var collected = [];
gulp.src('./client/**/*.html')
.pipe(frontMatter({ property: 'meta', remove: true }))
.pipe(through.obj(function(file, enc, callback) {
var css = file.meta;
collected.push(css);
}))
.pipe(gulp.dest('./build/templates'))
.on('end', function() {
var cssPath = ['build', 'assets', 'css', 'compiled.css'];
fs.writeFileSync(cssPath.join(path.sep), cssPath);
})
;
任务按预期方式工作(请注意,它是简化版本).一切都按预期工作,我得到了一个包含所有前端CSS的compiled.css
文件.但是,我发现不仅需要在我的常规CSS文件中使用Prefixer,而且还需要在这个新的named.css中使用Prefixer.所以我创建了一个prefix
任务:
The task works as expected (note, it's a simplified version). Everything works as expected and I get a compiled.css
file with all of the front-matter CSS. However, I found a need to use the Prefixer not only on my regular css file but on this new compiled.css as well. So I created a prefix
task:
gulp.task('prefix', ['compileCSS', 'copy'], function() {
gulp.src('./build/assets/css/*.css')
.pipe(autoprefixer({ browsers: ['last 3 versions'] }))
.pipe(gulp.dest('build'))
;
});
现在,问题在于on('end'
函数在所有任务(而不只是compileCSS
任务)的末尾运行.
Now, the problem is that the on('end'
function runs at the end of ALL the tasks, not just the compileCSS
task.
我的问题是,有没有办法为每个任务注入结束"类型的任务?还是有一种以某种方式使用流的方法(由于上一个任务不是实际的流,并且没有利用它,所以我不知道如何使用它.)
My question is, is there a way to inject an "on end" type task for each task? Or is there a way to use streams somehow (since the last task isn't an actual stream and doesn't take advantage of it, I don't see how).
推荐答案
这不是问题所在,我没有返回创建竞争条件的流,所以这样的修复起作用了:
It wasn't the sequence that was the problem, I wasn't returning the stream which created a race condition so a fix like this worked:
return gulp.src('./client/**/*.html')
.pipe(dosomething());
\\ all other code
我想问题是Gulp在开始下一个事件之前等待流完成.在不返回流或承诺的情况下,Gulp只会运行任务,而不会等待其完成.
I guess the problem was that Gulp waits for a stream to be done before setting off the next event. Without returning streams or promises, Gulp simply runs the task and doesn't wait for it to finish.
这篇关于您如何“结束"运行大口吃任务,但仅在当前任务结束时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!