I have have three gulp tasks, where the last task (allScripts
) runs the two dependent tasks first and then joins the resultant files from them.
I could, in the last task, delete the two result-files from the two first tasks and live happily ever after with the joined file.
But I was thinking, is it possible to avoid the two temporary files by somehow piping them into the allScripts
task "directly"?
gulp.task('firstGroup', function() {
return gulp.src('some/files/*.js')
.pipe(doSomething())
.pipe(concat('some-scripts.js'))
.pipe(gulp.dest('dest'));
});
gulp.task('secondGroup', function() {
return gulp.src('some/other/files/*.js')
.pipe(doSomethingElse())
.pipe(concat('some-other-scripts.js'))
.pipe(gulp.dest('dest'));
});
gulp.task('allScripts', ['firstGroup','secondGroup'], function() {
return gulp.src(['dest/some-scripts.js','dest/some-other-scripts.js'])
.pipe(concat('all-scripts.js'))
.pipe(gulp.dest('dest'))
// delete the two src-files
});
If everything can be a single task, you can use the
gulp-merge
plugin to combine multiple streams into one. There is also a solution below if the tasks need to stay separate, but please note that that method is a hack because it relies on a exposed property in Gulp.Without a hack solution, using the output from one task in another, would require intermediary storage, like what you are doing with a file.
Single task solution:
Here is a barebones demo using
gulp-merge
:In your case and using the code in your question it would look like:
Multiple task solution:
If your task structure is such that you can not merge them into a single task using the method above, this is your best bet. It is a bit hacky in the sense that it relies on
Gulp.tasks
which is a non-standard exposed property. There is no gurantee that this will work with future versions of Gulp (currently tested with Gulp v3.8.10).This snippet relies on the
event-stream
package because it is more robust and I use some of their utilities in therunTasksAndGetStreams
function.@MLM had the right idea with about combining streams.
But don't forget that Gulp's just Javascript.
Try this:
And probably name your tasks and their related functions a bit better than above.
That being said, it's probably still easier and more clear to delete the files in the last task.