I want to start the gulp.src thread, pass it to a function that creates a bunch of new threads, and then pass the result from them to gulp.dest . Below is what I have so far, but it obviously does not work, since I am passing the streams back to gulp.dest , which explodes because it expects a file, not a stream. So my question is: how to correctly return the n number of threads back to the original gulp stream so that they can continue working on the channel accordingly?
//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(); }); };
You can see this sample code that you can develop and play with this repo: https://github.com/chmontgomery/gulp-streams-to-stream
source share