4

The setup is as simple as this:

gulp.task('rev-js',  function() {
  return gulp.src('/js/main.js, {base: '.'})
      .pipe(newer('_build'))
      .pipe(rev())
      .pipe(gulp.dest('_build'))
      .pipe(rev.manifest())
      .pipe(gulp.dest('_build/rev/js'));
});

gulp-newer obviously doesn't work here since the destination file gets a different name. Any workaround to make gulp-newer (or gulp-changed) work in this case?

Tigran Petrossian
  • 1,158
  • 1
  • 7
  • 16
  • why would you combine two.. gulp-rev is used mostly during prod deployment and gulp-newer only during dev – harishr Jul 26 '15 at 15:51
  • @entre In my specific use case I absolutely need to combine two. – Tigran Petrossian Jul 26 '15 at 18:56
  • 2
    can you please explain your use case – harishr Jul 27 '15 at 04:53
  • Just to repeat @entre: It would really help if you explain your use case. Do you want to use newer to prevent unnecessary work done by the build or for some other reason? (keeping old file modification dates maybe?) – Simon Groenewolt Aug 03 '15 at 09:58
  • The setup is being used on a rather large static website, revving unchanged files for no reason causes updating few hundred .html files, thus making incremental builds take longer, plus FTP sync takes longer. – Tigran Petrossian Aug 03 '15 at 19:44

2 Answers2

1

In the gulp-newer options documentation I read that it supports passing in a configuration object instead of the destination. In that configuration object you can specify a mapping function from old to new files. So instead of

newer('_build')

you can write

newer({dest: '_build', map: mappingFn})

The mapping function takes the relative name of the file and expects it to return a translated name - see the index.js file. You can define a function that uses the previously generated rev-manifest.json manifest to look up the correct filename. Id put something along these lines in your build script (not tested):

gulp.task('rev-js',  function() {

  // get the existing manifest
  // todo: add logic to skip this if file doesn't exist
  var currentManifest = JSON.parse(fs.readFileSync('rev-manifest.json', 'utf8'));

  // mapping function for gulp-newer
  function mapToRevisions(relativeName) {
    return currentManifest[relativeName]
  }

  return gulp.src('/js/main.js, {base: '.'})
      .pipe(newer({dest: '_build', map: mapToRevisions}))
      .pipe(rev())
      .pipe(gulp.dest('_build'))
      .pipe(rev.manifest())
      .pipe(gulp.dest('_build/rev/js'));
});
Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
1

May I suggest gulp-newy in which you can manipulate the path and filename in your own function. Then, just use the function as the callback to the newy(). This gives you complete control of the files you would like to compare.

This will allow 1:1 or many to 1 compares.

newy(function(projectDir, srcFile, absSrcFile) {
  // do whatever you want to here. 
  // construct your absolute path, change filename suffix, etc. 
  // then return /foo/bar/filename.suffix as the file to compare against
}

enter image description here

dman
  • 10,406
  • 18
  • 102
  • 201