To compose streams, you pipe() the first to the second, but you also have to modify pipe() and unpipe() so they apply to the second stream. This way additional calls to pipe() on the composed stream will use the output of the second stream instead of the first.
function compose(s1, s2) {
s1.pipe(s2);
s1.pipe = function (dest) {
return s2.pipe(dest);
}
s1.unpipe = function (dest) {
return s2.unpipe(dest);
}
return s1;
}
var composed = compose(test(), test());
gulp.src('source/*')
.pipe( composed ) // src -> s1 -> s2
.pipe( ... ); // s2 -> ...