Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created January 12, 2015 16:56
Show Gist options
  • Save ChrisLTD/49bbe0ac17bf9880c2fb to your computer and use it in GitHub Desktop.
Save ChrisLTD/49bbe0ac17bf9880c2fb to your computer and use it in GitHub Desktop.
Gulpfile w/ cache buster replacement
var gulp = require('gulp');
var plumber = require('gulp-plumber'); // used for error catching during watch
var sass = require("gulp-sass");
var changed = require('gulp-changed'); // only move changed files
var replace = require("gulp-replace");
var concat = require("gulp-concat");
var sourcePaths = {
styles: ['./scss/*.scss'],
scripts: ['./js/savings_slider.js', './js/jw_custom.js'],
version: './cache-version.php'
};
var distPaths = {
styles: './css',
scripts: './js'
};
gulp.task('styles', ['version'], function() {
return gulp.src( sourcePaths.styles )
.pipe(plumber())
.pipe(changed(distPaths.styles))
.pipe(sass({ errLogToConsole: true }))
.pipe(gulp.dest( distPaths.styles ));
});
gulp.task('scripts', ['version'], function() {
return gulp.src( sourcePaths.scripts )
.pipe(plumber())
.pipe(concat('scripts.js'))
.pipe(gulp.dest( distPaths.scripts ));
});
gulp.task('version', function() {
// Do an in-place replace
var random = Math.floor((Math.random() * 9999) + 1);
return gulp.src( sourcePaths.version )
.pipe(replace(/'CACHE_BUSTER_VERSION', '(\d+)'/g, "'CACHE_BUSTER_VERSION', '"+ random +"'"))
.pipe(gulp.dest('./'));
});
// Rerun the task when a file changes
gulp.task('watch', function(){
gulp.watch(sourcePaths.styles, ['styles']);
gulp.watch(sourcePaths.scripts, ['scripts']);
});
gulp.task('build', ['styles', 'scripts']);
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['build', 'watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment