sindresorhus / gulp-rev

Static asset revisioning by appending content hash to filenames: `unicorn.css` → `unicorn-d41d8cd98f.css`
MIT License
1.54k stars 217 forks source link

Question: How to remove original file? #50

Closed steverandy closed 10 years ago

steverandy commented 10 years ago

Want to keep just files with fingerprint. Is there a way to remove original files without creating new task?

secretfader commented 10 years ago

I would love to see a how-to on this as well.

bobthecow commented 10 years ago

This should do it:

var gulp    = require('gulp');
var rev     = require('gulp-rev');
var fs      = require('fs');
var through = require('through2');
var gutil   = require('gulp-util');
var log     = gutil.log;
var colors  = gutil.colors;

var rmOrig = function() {
  return through.obj(function(file, enc, cb) {
    this.push(file); // We'll just pass this file along

    if (!file.revOrigPath) {
      return cb(); // Nothing to remove :)
    }

    log(colors.red('DELETING'), file.revOrigPath);
    fs.unlink(file.revOrigPath, function(err) {
      // TODO: emit an error if err
      cb();
    });
  });
};

gulp.task('cachebust', function() {
  return gulp.src('public/assets/**/*.*')
    .pipe(rev())
    .pipe(gulp.dest('public/assets'))
    .pipe(rmOrig());
});

Note that I didn't actually run this, but it's the right idea :)

secretfader commented 10 years ago

@bobthecow Thanks! I can confirm that it works.

steverandy commented 10 years ago

Thanks @bobthecow. Works for me.

secretfader commented 10 years ago

I just integrated this into my gulp-app-skeleton: https://github.com/originalmachine/gulp-app-skeleton

bobthecow commented 10 years ago

Thanks for sharing @nicholaswyoung

FrankyMartz commented 10 years ago

@bobthecow Thanks! Worked with minor adjustment though. Needed to move the push towards the bottom of the rmOrig function.


var rmOrig = function() {
  return through.obj(function(file, enc, cb) {

    if (file.revOrigPath) {
      log(colors.red('DELETING'), file.revOrigPath);
      fs.unlink(file.revOrigPath, function(err) {
        // TODO: emit an error if err
      });
    }

    this.push(file); // Pass file when you're done
    return cb() // notify through2 you're done
  });
};

Found the original implementation skipping a few files. I assume because the files were being passed along before function completion.

@nicholaswyoung Noticed you're using a similar implementation... might want to look into that.

bobthecow commented 10 years ago

@FrankyMartz You're going to get weirdness from calling cb twice when file.revOrigPath is truthy.

FrankyMartz commented 10 years ago

Oops, forgot to take that out! Thanks. :beer: fixed.

secretfader commented 10 years ago

Thanks, @frankymartz. I'm going to take a look at my implementation now.