Lego2012
10/14/2016 - 10:58 AM

Same task, different configurations https://fettblog.eu/gulp-recipes-part-1/

Same task, different configurations https://fettblog.eu/gulp-recipes-part-1/

var config = [
	{
		src: 'project-one/scripts/**/*.js',
		name: 'p1.bundle.js',
		dest: 'public/compiled'
	},
	{
		src: 'project-two/scripts/**/*.js',
		name: 'p2.bundle.js',
		dest: 'public/compiled'
	},
	{
		src: 'project-three/scripts/**/*.js',
		name: 'p3.bundle.js',
		dest: 'private/compiled'
	},
	{
		src: 'project-four/scripts/**/*.js',
		name: 'p4.bundle.js',
		dest: 'private/compiled'
	}
];

// We want to run all of those to a pipeline of task which is absolutely identical. The only things different are inputs and outputs. We can achieve this by creating a so called stream array:

var gulp   = require('gulp');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
// this node module will do the trick
var merge  = require('merge2');

gulp.task('scripts', function() {
	// we use the array map function to map each
	// entry in our configuration array to a function
	var tasks = config.map(function(entry) {
		// the parameter we get is this very entry. In
		// that case, an object containing src, name and
		// dest.
		// So here we create a Gulp stream as we would
		// do if we just handle one set of files
		return gulp.src(entry.src)
			.pipe(concat())
			.pipe(uglify())
			.pipe(rename(entry.name))
			.pipe(gulp.dest(entry.dest))
	});
	// tasks now includes an array of Gulp streams. Use
	// the `merge-stream` module to combine them into one
	return merge(tasks);
});

// This pattern can be used by many problems. We had something similar last time when we created multiple Browserify bundles.