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 that all images found in this directory are optimized and recorded in it. But I do not quite understand how it could be done.
Answer 1, Authority 100%
Gulp.Dest
supports the use of a function as the first argument. This feature accepts a file as an argument as Vinyl-object
. Returning line is used as a directory for saving.
Try this:
gulp.dest (File {
Return file.Base;
})
Answer 2, Authority 14%
Specify in Dest the same directory as in SRC
return gulp.src ('./ images / *.jpg')
.pipe (IMAGEMINJPEGTRAN ({PROGRESSIVE: TRUE}) ())
.pipe (gulp.dest ('./ images'));
});
Answer 3
Here is another solution to my task. It is longer and less convenient than @avb but works about 20% faster and gives a more organized log, for each directory.
var merge = require ('merge-stream');
var falders =.
[
"./pictures/picmanager/",
"./pictures/news/",
"./pictures/payment/",
"./pictures/manager/",
"./pictures/news/",
"./pictures/payment/",
"./pictures/product/original/"
"./pictures/product/big/",
"./pictures/product/middle/",
"./pictures/product/xsmall/"
"./pictures/shipping/"
]
Gulp.Task ('OptimizeImgs', Function () {
Var Tasks = Folders.map (FUNCTION (ELEMENT) {
Return Gulp.Src (Element + '*')
.pipe (Imagemin ({
Progressive: True,
SVGOPLUGINS: [{
RemoveViewBox: False.
}],
Use: [PNGQUANT ()]
}))
.pipe (Gulp.Dest (Element));
});
RETURN MERGE (TASKS);
});