Skip to content

Instantly share code, notes, and snippets.

@IvanTorresEdge
Created September 29, 2017 20:46
Show Gist options
  • Save IvanTorresEdge/4f65cb8c109077c87da1e3939c81a89b to your computer and use it in GitHub Desktop.
Save IvanTorresEdge/4f65cb8c109077c87da1e3939c81a89b to your computer and use it in GitHub Desktop.
Incremental builds with Gulp

Incremental rebuilding, including operating on full file sets

(From: https://raw.githubusercontent.com/gulpjs/gulp/master/docs/recipes/incremental-builds-with-concatenate.md)

The trouble with incremental rebuilds is you often want to operate on all processed files, not just single files. For example, you may want to lint and module-wrap just the file(s) that have changed, then concatenate it with all other linted and module-wrapped files. This is difficult without the use of temp files.

Use gulp-cached and gulp-remember to achieve this.

var gulp = require('gulp');
var header = require('gulp-header');
var footer = require('gulp-footer');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint');
var cached = require('gulp-cached');
var remember = require('gulp-remember');

var scriptsGlob = 'src/**/*.js';

gulp.task('scripts', function() {
  return gulp.src(scriptsGlob)
      .pipe(cached('scripts'))        // only pass through changed files
      .pipe(jshint())                 // do special things to the changed files...
      .pipe(header('(function () {')) // e.g. jshinting ^^^
      .pipe(footer('})();'))          // and some kind of module wrapping
      .pipe(remember('scripts'))      // add back all files to the stream
      .pipe(concat('app.js'))         // do things that require all files
      .pipe(gulp.dest('public/'));
});

gulp.task('watch', function () {
  var watcher = gulp.watch(scriptsGlob, ['scripts']); // watch the same files in our scripts task
  watcher.on('change', function (event) {
    if (event.type === 'deleted') {                   // if a file is deleted, forget about it
      delete cached.caches.scripts[event.path];       // gulp-cached remove api
      remember.forget('scripts', event.path);         // gulp-remember remove api
    }
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment