Skip to content

Instantly share code, notes, and snippets.

@kejsper
Created May 10, 2017 13:57
Show Gist options
  • Save kejsper/b30c8de2ab7d5a3a9431223e1c72b6ed to your computer and use it in GitHub Desktop.
Save kejsper/b30c8de2ab7d5a3a9431223e1c72b6ed to your computer and use it in GitHub Desktop.
Gulpfile.js CSS Hashing
var gulp = require('gulp');
var clean = require('gulp-clean');
var rev = require('gulp-rev');
var inject = require('gulp-inject');
var fs = require('fs');
var jsonCss;
gulp.task('default', ['delete']);
gulp.task('cssHash', function() {
return gulp.src('./css/styles.css')
.pipe(gulp.dest('./css/dist'))
.pipe(rev())
.pipe(gulp.dest('./css/dist'))
.pipe(rev.manifest())
.pipe(gulp.dest('./css/dist/'));
});
gulp.task('inject', ['cssHash'], function() {
jsonCss = JSON.parse(fs.readFileSync('./css/dist/rev-manifest.json'));
var target = gulp.src('./index.html');
var source = gulp.src('./css/dist/' + jsonCss['styles.css'], {read: false});
return target.pipe(inject(source, {relative: true}))
.pipe(gulp.dest(''));
});
gulp.task('delete', ['inject'], function () {
return gulp.src(['css/dist/*.css', '!css/dist/' + jsonCss['styles.css']])
.pipe(clean());
});
gulp.task('watch', function() {
gulp.watch('css/styles.css', ['delete']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment