B
B
biz532020-11-02 11:25:39
css
biz53, 2020-11-02 11:25:39

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

Question
There is a galp file, imagemin is connected in it and, accordingly, a watcher for tracking file updates. Is there any way to make the compress task (see below) work only for new/changed files, and skip old ones (already processed)? The question arose due to the large number of pictures. The last execution of the task took 3 minutes, and even more pictures are planned in the future.
Imagemin setup example:

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())
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Pershin, 2020-11-02
@biz53

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
        }))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question