Tigran Petrossian
Tigran Petrossian

Reputation: 1168

How to make gulp-newer work with gulp-rev?

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?

Upvotes: 4

Views: 791

Answers (2)

dman
dman

Reputation: 11073

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

Upvotes: 1

Simon Groenewolt
Simon Groenewolt

Reputation: 10665

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'));
});

Upvotes: 1

Related Questions