gulp.js – 如何将多个stream返回到主stream?

我想启动一个gulp.srcstream,把它传递给一个创build一堆新stream的函数,然后把这些stream的结果传给gulp.dest 。 下面是我到目前为止,但显然不工作,因为我正在pipestream回到gulp.dest ,因为它期待一个文件,而不是一个stream。 所以我的问题是:如何正确地将n个stream返回到gulp的原始stream,以便它们可以继续适当的下降pipe道?

 //gulpfile.js var gulp = require('gulp'), bundle = require('./lib/bundle.js'); gulp.task('bundle', function() { return gulp.src('./bundle.config.js') .pipe(bundle()) .pipe(gulp.dest('./public')); }); 

 //bundle.config.js module.exports = { bundle: { main: { js: [ './content/js/foo.js', './content/js/baz.js' ], css: [ './content/**/*.css' ], resources: './content/**/*.{png,svg}' }, other: { js: './content/js/other.js', css: '', resources: '' } } }; 

 //bundle.js var gulp = require('gulp'), through = require('through2'), concat = require('gulp-concat'); module.exports = function () { return through.obj(function (file, enc, cb) { var config; try { config = require(file.path); // get config file } catch (e) { this.emit('error', e); return cb(); } var streams = []; for (var key in config.bundle) { var bundle = config.bundle[key]; streams.push( gulp.src(bundle.js, {base: '.'}) .pipe(concat(key + '.js')) ); streams.push( gulp.src(bundle.css, {base: '.'}) .pipe(concat(key + '.css')) ); streams.push( gulp.src(bundle.resources, {base: '.'}) //.pipe(something()) ); } for (var i = 0; i < streams.length; i++) { // This causes an error in `gulp.dest` because we're returning the stream, not the file. // Instead, how do I resolve each of the individual streams and push the results back to the main stream?? this.push(streams[i]); } cb(); }); }; 

你可以看到这个示例代码,你可以在这个repo上分享和使用: https : //github.com/chmontgomery/gulp-streams-to-stream

您可以使用合并stream连接stream

 var gulp = require('gulp'); var merge = require('merge-stream'); gulp.task('bundle', function () { var paths = [ { src: 'src/admin/**', dest: './build/admin' }, { src: 'src/public/**', dest: './build' } ]; var tasks = paths.map(function (path) { return gulp.src(path.src).pipe(gulp.dest(path.dest)); } return merge(tasks); }; 

等到你合并了这些stream之后。

 var gulp = require('gulp'); var es = require('event-stream'); var concat = require('gulp-concat'); gulp.task('bundle', function(cb) { //Sorry, forgot the bundling var paths = [{ path: 'src/admin/**', filename: 'one.file' }, { path: 'src/public/**', filename: 'two.file' }]; var tasks = paths.map(function(path) { return gulp.src(path.path) .pipe(concat(path.filename)); } es.merge.apply(null, tasks) .pipe(gulp.dest('./dist/dest')) .on('end', cb); });