gulpgulp-imagemin

Usage of Gulp Since or 'vinyl-filter-since'


I'm trying to build a simple image optimizer that only triggers on images added "since" 7 days ago. The purpuses is to run it on a webserver each weekend to optimize images added through the week.

So, far I got this:

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');

gulp.task('default', function () {
    return gulp.src('images/**/*.{jpeg,jpg,png,gif}', {base: './'})
        .pipe(imagemin([
        imagemin.jpegtran({progressive: true})
        ], {
            verbose: true
        }))
        .pipe(gulp.dest('./'));
});

This script now runs on all images in the "images" folder. I've tried with the option "since" on gulp.src with no luck. Does anybody have any advice?

Thank you!


Solution

  • You don't show how you tried the since option, so we can't help you there. The following is untested but something like this:

    const today = Date.now();
    const sevenDays = 604800000;  // in milliseconds
    
    gulp.task('default', function () {
      return gulp.src('images/**/*.{jpeg,jpg,png,gif}', {base: './', since: (today - sevenDays)})
    

    Get today's date, subtract 7 days (604800000 milliseconds) from it and use that as your since: value