It depends if you are using gulp 4.0.2 or 3.9.1 (solution for both below)
If using gulp 4.0.2 you want to use the gulp.src
function's since
option and gulp.lastRun
const images = () => {
return gulp.src(paths.images.src, {since: gulp.lastRun(images)})
.pipe(imagemin({optimizationLevel: 5}))
.pipe(gulp.dest(paths.images.dest));
}
Full docs here
If using gulp 3.9.1 I would recommend using gulp-cached and gulp-remember - example:
const gulp = require('gulp')
const cache = require('gulp-cached')
const cleanCSS = require('gulp-clean-css')
const remember = require('gulp-remember')
const sass = require('gulp-sass')(require('node-sass'))
const sourcemaps = require('gulp-sourcemaps')
// const tap = require('gulp-tap')
// const util = require('gulp-util')
gulp.task('scss', () => {
return gulp.src(paths.scss.basic)
// .pipe(tap(file => util.log('scss', file.path)))
.pipe(cache('scss1')) // 81 FILES
.pipe(sourcemaps.init())
.pipe(remember('scss1')) // 81 FILES
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(cache('scss2')) // 10 FILES
.pipe(cleanCSS())
.pipe(remember('scss2')) // 10 FILES
.pipe(sourcemaps.write('./'))
.pipe(cache('scss3')) // 5 FILES
.pipe(gulp.dest('./../../css'))
})
This is quite a complex example - generally gulp-cached
should only be used where there is a 1:1 mapping in the stream. However, if you have a file count that changes as the stream progresses (e.g. when using gulp-concat
) use gulp-remember
in conjunction with gulp-cached
. In the example above, gulp-cached
is used when there is a new file count and gulp-remember
is used before the file count changes. If you don't know the file count at various places in the stream then use util.log
to see what's going on. Notice that the cache name changes, the cache and remember statements are paired by cache name!
NB file count here refers to the number of files passing through the stream before caching
Other gulp plugins that try to do the same thing but that don't improve things if file count changes are gulp-changed and gulp-newer. For a full gulp 3.9.1 list see here
NB gulp-changed
supports gulp 3.9.1 ([email protected]
) and gulp 4.0.2 (gulp-changed@latest
)