Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gulp.dest() in same directory as pipe inputs?

Tags:

gulp

I need all the found images in each of the directories to be optimized and recorded into them without setting the path to the each folder separately. I don't understand how to make that.

var gulp = require('gulp'); var imageminJpegtran = require('imagemin-jpegtran');  gulp.task('optimizeJpg', function () {  return gulp.src('./images/**/**/*.jpg')     .pipe(imageminJpegtran({ progressive: true })())     .pipe(gulp.dest('./')); }); 
like image 314
Andrey Vaganov Avatar asked Apr 22 '15 09:04

Andrey Vaganov


People also ask

What is Gulp DEST?

dest() Creates a stream for writing Vinyl objects to the file system.

What is pipe in Gulp?

Gulp plugins are Node Transform Streams that encapsulate common behavior to transform files in a pipeline - often placed between src() and dest() using the . pipe() method. They can change the filename, metadata, or contents of every file that passes through the stream.

How do I copy files using Gulp?

var copy = require('gulp-copy'); gulp. task('copy-resources', function() { return gulp. src('./src/img/*. png') .

Which of the following Gulp script is used to delete a specific folder?

I use the del package to delete a folder: gulp. task('clean', function(){ return del('dist/**/*', {force:true}); });


1 Answers

Here are two answers.
First: It is longer, less flexible and needs additional modules, but it works 20% faster and gives you logs for every folder.

var merge = require('merge-stream');  var folders = [     "./pictures/news/",     "./pictures/product/original/",     "./pictures/product/big/",     "./pictures/product/middle/",     "./pictures/product/xsmall/",     ... ];  gulp.task('optimizeImgs', function () {      var tasks = folders.map(function (element) {          return gulp.src(element + '*')             .pipe(sometingToDo())             .pipe(gulp.dest(element));      });      return merge(tasks);  }); 

Second solution: It's flexible and elegant, but slower. I prefer it.

return gulp.src('./pictures/**/*')     .pipe(somethingToDo())     .pipe(gulp.dest(function (file) {         return file.base;     })); 
like image 105
Andrey Vaganov Avatar answered Sep 22 '22 17:09

Andrey Vaganov