gulp-rev-outdated
Old static asset revision files filter.
$ npm install --save-dev gulp-rev-outdated
We can use gulp-rev to cache-bust several assets. Every modification of source files caused a new revisioned asset creation. In case of using separate http://static.exsample.com/ domain for distributing static assets we have some problem with a lot of accumulated revisioned asset files. If we have several different frontends (e.q. [www-1.exsample.com, www-2.exsample.cpm, ... www-12.exsample com]) worked with different software releases, We can't remove all revisioned asset files on static.exsample.com. We need to save number of recent revisioned assets. gulp-rev-outdated filter revisioned assets and exclude parametrised quantity of recent files for removing.
Takes one parameter [ keepQuantity ] - number of saved recent files. Default value == 2.
var gulp = require('gulp');
var gutil = require('gulp-util');
var rimraf = require('rimraf');
var revOutdated = require('gulp-rev-outdated');
var path = require('path');
var through = require('through2');
function cleaner() {
return through.obj(function(file, enc, cb){
rimraf( path.resolve( (file.cwd || process.cwd()), file.path), function (err) {
if (err) {
this.emit('error', new gutil.PluginError('Cleanup old files', err));
}
this.push(file);
cb();
}.bind(this));
});
}
gulp.task('clean', function() {
gulp.src( ['dist/js/vendors*.js'], {read: false})
.pipe( revOutdated(1) ) // leave 1 latest asset file
.pipe( cleaner() );
gulp.src( ['dist/js/bundle*.js'], {read: false})
.pipe( revOutdated(3) ) // leave 3 recent assets
.pipe( cleaner() );
gulp.src( ['dist/css/*.css'], {read: false})
.pipe( revOutdated() ) // leave 2 recent assets (default value)
.pipe( cleaner() );
return;
});
It's also possible to pass in all your asset files at once:
[...]
gulp.task('clean', function() {
gulp.src( ['dist/**/*.*'], {read: false})
.pipe( revOutdated(1) ) // leave 1 latest asset file for every file name prefix.
.pipe( cleaner() );
return;
});
gulp.src option read false prevents gulp to read the contents of the file and makes this task a lot faster. If you need the file and it's contents after cleaning in the same stream, do not set the read option to false.