In Gulp, how do I only run a task on one file if any of multiple files are newer?

You can write your own through/transform stream to handle the condition like so:

// Additional core libs needed below
var path = require('path');
var fs = require('fs');
// Additional npm libs
var newer = require('gulp-newer');
var through = require('through');
var File = require('vinyl');

gulp.task('build_lib', function() {
  return gulp.src(["app/**/*.ts"])
    .pipe(newer("out/outputLib.js"))
    .pipe(through(function(file) {
      // If any files get through newer, just return the one entry
      var libsrcpath = path.resolve('app', 'scripts', 'LibSource.ts');
      // Pass libsrc through the stream
      this.queue(new File({
        base: path.dirname(libsrcpath),
        path: libsrcpath,
        contents: new Buffer(fs.readFileSync(libsrcpath))
      }));
      // Then end this stream by passing null to queue
      // this will ignore any other additional files
      this.queue(null);
    }))
    .pipe(typescript({
      declaration: true,
      sourcemap: true,
      emitError: true,
      safe: true,
      target: "ES5",
      out: "outputLib.js"
    }))
    .pipe(gulp.dest('out/'));
});

Tags:

Gulp