How to teach imagemin (gulp) to compress only new photos and not touch already processed ones?

  • 0
    Question
    There is a galp file, imagemin is connected in it and, accordingly, a watchdog for tracking file updates. Is it possible to somehow make the compress task (see below) work only for new / changed files, and skip old (already processed) files? The question arose because of the large number of pictures. The last execution of the task took 3 minutes, while more pictures are planned in the future.
    Example for configuring imagemin:
    function compress () {
      return gulp.src(src.img)
        .pipe(
            webp({
                quality: 70
            }))
        .pipe(gulp.dest(`./build/img/`))
        .pipe(gulp.src(src.img))
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],
            interlaced: true,
            optimizationlevel: 5
        }))
        .pipe(gulp.dest(`./build/img/`))
        .pipe(browserSync.stream())
    }
    JavaScript Joseph Baxter, Jul 6, 2019

  • 1 Answers
  • 0
    We include the gulp-changed library

    const changed = require ('gulp-changed');



    And first of all, before compressing, we check the time of the last change, then we compare the hash:



    .pipe(changed(dir.output + '/images', {
    hasChanged: changed.compareLastModifiedTime
    }))
    .pipe(changed(dir.output + '/images', {
    hasChanged: changed.compareContents
    }))
    Ava Hutchinson

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!