Speeding Up Gulp Watch with Incremental Builds
Learn how to make Gulp watch rebuild only the changed files using gulp.lastRun() for faster feedback loops.
22 Jun 2026, 04:04 UTC

Problem: Slow feedback when editing a single file
In a medium‑sized web project the default gulp.watch task re‑processes every source file on each change, even when only one JavaScript module was edited. The watch loop becomes a bottleneck, delaying the instant feedback developers rely on.
Thesis: Use gulp.lastRun() with gulp.watch to rebuild only the files that changed since the last successful run, keeping the pipeline fast and readable.
How Gulp 4/5 expresses task order
Gulp replaced the old dependency‑array syntax with explicit composition functions:
gulp.series(t1, t2)– runt1thent2sequentially.gulp.parallel(t1, t2)– runt1andt2concurrently.- Both can be nested to describe any build graph.
A task signals completion by returning a stream, promise, event emitter, or calling the async callback. Forgetting to signal makes Gulp think the task is still running, causing hangs or silent termination.
Worked example: incremental scripts task
const { src, dest, series, parallel, watch, lastRun } = require('gulp');
const babel = require('gulp-babel');
function clean() {
return src('dist', { read: false, allowEmpty: true }).pipe(require('del')());
}
function scripts() {
return src('src/**/*.js', { since: lastRun(scripts) })
.pipe(babel({ presets: ['@babel/preset-env'] }))
.pipe(dest('dist/js'));
}
function styles() {
return src('src/**/*.css')
.pipe(dest('dist/css'));
}
exports.default = series(clean, parallel(scripts, styles));
// Watch only changed JS files
watch('src/**/*.js', { since: lastRun(scripts) }, scripts);
When you edit src/utils/helper.js and save, the since: lastRun(scripts) option tells gulp.src to pass through only that file (based on its timestamp). The Babel step runs once, the updated file appears in dist/js, and the watch loop finishes quickly.
Trade‑off and limitation
Timestamp‑based incrementality assumes a file’s modification time changes when its content changes. If you restore a file from a backup that preserves the original mtime, or a generator writes a file with an unchanged timestamp, Gulp will skip it and you’ll see stale output. The safest practice is to keep a clean build task (e.g., gulp clean) and run it periodically or before releases.
Actionable closing
Add the since: lastRun(task) option to any watch that processes files written to disk. Verify the behavior by touching a single source file and checking your plugin’s log—only that file should appear. Keep a full clean task as a fallback, and you’ll get fast, reliable feedback without rewriting your entire gulpfile.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.