0

I'm having trouble getting gulp-imagemin 5.2.1 to use a different plugin from the defaults. I need to compress jpegs more than the default jpegtran plugin does with its lossless compression. I wanted to try using imagemin-jpeg-recompress 5.1.0. Here is my gulp task:

var imagemin = require('gulp-imagemin');
var imageminJpegRecompress = require('imagemin-jpeg-recompress');
gulp.task('imagemin', function(){
    return gulp.src(['./app/img/**'], {base:'app'})
    .pipe(plumber({errorHandler: onerror}))
    .pipe(imagemin({
      plugins:[imageminJpegRecompress({
        quality: 'low'
      })]
    }))
    .pipe(gulp.dest('dist/app/'));
});

When I run this, I get exactly the same images I got before no changes in how things are compressed. It looks like it doesn't even see the "plugins" option or maybe it's still running all of the default plugins after the jpegRecompress. How should I be calling the imagemin plugins in my gulp file?

Note: They did change the "plugins" option. The "plugins" option used to be named 'use".

Brian
  • 1,675
  • 3
  • 19
  • 29

1 Answers1

0

You are correct that the "use" option has been renamed to "plugins", however after looking into this issue myself, I realised that gulp-imagemin (the one you are using) has a slightly different syntax to standalone imagemin with respect to plugin configuration.*

This should work:

gulp.task('imagemin', function() {
    return gulp.src(['./app/img/**'], {base:'app'})
    .pipe(plumber({errorHandler: onerror}))
    .pipe(imagemin([
        imagemin.gifsicle(),
        imageminJpegRecompress({ quality: 'low' }),
        imagemin.optipng(),
        imagemin.svgo()
    ]))
    .pipe(gulp.dest('dist/app'));
});

Note that since I am passing in a custom plugins array, I am declaring plugins for the other filetypes, per the instructions on the gulp-imagemin Github page. In this case, I just declare the defaults, except jpeg-recompress, whose options I pass in an object.

Sources:
API Change with diff example
Relevant discussion on the project's issue tracker

*Aside on versions: The latest version of imagemin is 5.2.x, whereas gulp-imagemin has separate versioning, at 3.0.x. More confusingly, imagemin-jpeg-recompress is 5.1.0; these combinations don't help much, but might help when looking up the diffs on each project's page.

fpapado
  • 41
  • 1
  • 5