Gulp-rev replacing files with generated hash

1.7k views Asked by At

I started using Gulp and I want to create a task that renames the files(js,css) with hash and then inject them back into the html.

I actually have two functions that do what I want, but I can't figure out how to delete the old file and keep just the new js and css hashed files.

Some could give me a hint? This is my code right now:

obs: 'config' variable has the folder locations

gulp.task('prod:rev',['prod:revision'],function() { 
  var manifest = gulp.src(config.prod.manifest);
  return gulp
    .src(config.prod.header)
    .pipe(plugin.revReplace({manifest: manifest}))
    .pipe(gulp.dest('../'));
});

gulp.task('prod:revision',function(){
  var revJs = gulp
   .src(config.scripts.build+'*.js')
   .pipe(plugin.rev())
   .pipe(gulp.dest(config.scripts.build))
   .pipe(plugin.rev.manifest({merge: true}))
   .pipe(gulp.dest(config.prod.path));

  var revCss = gulp
   .src(config.styles.build+'*.css')
   .pipe(plugin.rev())
   .pipe(gulp.dest(config.styles.build))
   .pipe(plugin.rev.manifest({merge: true}))
   .pipe(gulp.dest(config.prod.path));

  return merge(revJs,revCss);
3

There are 3 answers

0
Pedro Bezanilla On

try putting this after calling .pipe(rev())

const revDelete = require('gulp-rev-delete-original');

... .pipe(revDelete()) ...

That will delete the 'unhashed' files.

0
skube On

According to the gulp-rev readme.md it sounds like you might want to also use gulp-rev-delete-original to delete the unhashed originals.

0
st2rseeker On

I have currently an open question on a similar issue - I get the files revved properly, but fail to inject them in the html with gulp-useref (you can check the supposed-to-work method in my question regarding useref). I suspect some foul play with the directories I use, but for now I've chosen to use a different cache-busting scheme:

var cb = Math.random();

....
.pipe($.replace('dist/css/lib.css', 'dist/css/lib.css?cb=' + cb))
....

It may not be the answer you want, but it may be an answer you need. :)