How can I make gulp.dest() write to the same directory as the processed file?

Question:

Here is an example

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('./'));
});

I need all found images in this directory to be optimized and written to it. But I don't quite understand how it could be done.

Answer:

gulp.dest supports using a function as the first argument. This function takes as argument a file as a vinyl-объект . The string it returns is used as the save directory.

Try like this :

gulp.dest(function(file){
    return file.base;
})
Scroll to Top