Closed steverandy closed 10 years ago
I would love to see a how-to on this as well.
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 :)
@bobthecow Thanks! I can confirm that it works.
Thanks @bobthecow. Works for me.
I just integrated this into my gulp-app-skeleton: https://github.com/originalmachine/gulp-app-skeleton
Thanks for sharing @nicholaswyoung
@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.
@FrankyMartz You're going to get weirdness from calling cb twice when file.revOrigPath is truthy.
Oops, forgot to take that out! Thanks. :beer: fixed.
Thanks, @frankymartz. I'm going to take a look at my implementation now.
Want to keep just files with fingerprint. Is there a way to remove original files without creating new task?