Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a Gulp task with multiple sources and multiple destinations?

I have the following in my gulpfile.js:

   var sass_paths = [         './httpdocs-site1/media/sass/**/*.scss',         './httpdocs-site2/media/sass/**/*.scss',         './httpdocs-site3/media/sass/**/*.scss'     ];  gulp.task('sass', function() {     return gulp.src(sass_paths)         .pipe(sass({errLogToConsole: true}))         .pipe(autoprefixer('last 4 version'))         .pipe(minifyCSS({keepBreaks:true}))         .pipe(rename({ suffix: '.min'}))         .pipe(gulp.dest(???)); }); 

I'm wanting to output my minified css files to the following paths:

./httpdocs-site1/media/css ./httpdocs-site2/media/css ./httpdocs-site3/media/css 

Am I misunderstanding how to use sources/destinations? Or am I trying to accomplish too much in a single task?

Edit: Updated output paths to corresponding site directories.

like image 594
David Angel Avatar asked Nov 06 '14 16:11

David Angel


People also ask

What is Gulp Gulpfile?

Gulpfile explained A gulpfile is a file in your project directory titled gulpfile. js (or capitalized as Gulpfile. js , like Makefile), that automatically loads when you run the gulp command.

How do you write a gulp task?

gulp. task('my-custom-task', function () { myCustomFunction('foo', 'bar'); }); If your function does something asynchronously, it should call a callback at the end, so gulp is able to know when it's done: gulp.

How does a gulp work?

Gulp is a task runner that uses Node. js as a platform. Gulp purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. It builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.


2 Answers

I guess that the running tasks per folder recipe may help.

Update

Following the ideas in the recipe, and oversimplifying your sample just to give the idea, this can be a solution:

var gulp = require('gulp'),     path = require('path'),     merge = require('merge-stream');  var folders = ['httpdocs-site1', 'httpdocs-site2', 'httpdocs-site3'];  gulp.task('default', function(){      var tasks = folders.map(function(element){         return gulp.src(element + '/media/sass/**/*.scss', {base: element + '/media/sass'})             // ... other steps ...             .pipe(gulp.dest(element + '/media/css'));     });      return merge(tasks); }); 
like image 57
Ghidello Avatar answered Sep 18 '22 14:09

Ghidello


you are going to want to use merge streams if you would like to use multiple srcs but you can have multiple destinations inside of the same one. Here is an example.

var merge = require('merge-stream');   gulp.task('sass', function() {    var firstPath = gulp.src(sass_paths[0])                .pipe(sass({errLogToConsole: true}))                .pipe(autoprefixer('last 4 version'))                .pipe(minifyCSS({keepBreaks:true}))                .pipe(rename({ suffix: '.min'}))                .pipe(gulp.dest('./httpdocs-site1/media/css'))                .pipe(gulp.dest('./httpdocs-site2/media/css'));    var secondPath = gulp.src(sass_paths[1])                .pipe(sass({errLogToConsole: true}))                .pipe(autoprefixer('last 4 version'))                .pipe(minifyCSS({keepBreaks:true}))                .pipe(rename({ suffix: '.min'}))                .pipe(gulp.dest('./httpdocs-site1/media/css'))                .pipe(gulp.dest('./httpdocs-site2/media/css'));    return merge(firstPath, secondPath); }); 

I assumed you wanted different paths piped here so there is site1 and site2, but you can do this to as many places as needed. Also you can specify a dest prior to any of the steps if, for example, you wanted to have one dest that had the .min file and one that didn't.

like image 28
ckross01 Avatar answered Sep 22 '22 14:09

ckross01