Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Be sure to replace `mytheme` with your theme's directory name on lines 59 and 62
var gulp = require('gulp');
var less = require('gulp-less');
var minifycss = require('gulp-minify-css');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var replace = require('gulp-replace');
var notify = require('gulp-notify');
gulp.task('less', function() {
gulp
.src('less/style.less')
.pipe(less().on("error", notify.onError(function (error) {
return "Error compiling LESS: " + error.message;
})))
// .pipe(minifycss())
.pipe(gulp.dest('dist/css'))
.pipe(notify({
message: 'Successfully compiled LESS'
}));
});
gulp.task('lint', function() {
gulp
.src('js/script.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('js', function() {
gulp
.src([
'components/jquery/dist/jquery.min.js',
'components/bootstrap/js/transition.js',
'components/bootstrap/js/collapse.js',
'components/bootstrap/js/carousel.js',
'components/bootstrap/js/dropdown.js',
'components/bootstrap/js/modal.js',
'js/script.js'
])
.pipe(concat('script.js'))
// .pipe(uglify())
.pipe(gulp.dest('dist/js'))
.pipe(notify({
message: 'Successfully compiled JS'
}));
});
var hashes = {};
gulp.task('hash', function(){
hash('dist/js/script.js');
hash('dist/css/style.css');
})
gulp.task('bust', function(){
gulp.src(['../../design/responsive/layout/local.xml'])
.pipe(replace(/(dist\/js\/script\.js)[^<]*/, '$1?v='+hashes['dist/js/script.js']))
.pipe(replace(/(dist\/css\/style\.css)[^<]*/, '$1?v='+hashes['dist/css/style.css']))
.pipe(gulp.dest('../../design/responsive/layout'))
})
function hash(filename) {
var crypto = require('crypto');
var fs = require('fs');
var shasum = crypto.createHash('sha1');
hashes[filename] = hashes[filename] ? hashes[filename] : {};
var s = fs.ReadStream(filename);
s.on('data', function(d) {
shasum.update(d);
});
s.on('end', function() {
hashes[filename] = shasum.digest('hex').substr(0,8);
gulp.run('bust')
});
}
gulp.task('default', ['less', 'lint', 'js', 'hash'], function() {
gulp.watch('less/**/*.less', function() {
gulp.run('less');
gulp.run('hash');
});
gulp.watch('js/**/*.js', function() {
gulp.run('lint');
gulp.run('js');
gulp.run('hash');
});
});
{
"devDependencies": {
"gulp": "~3.4.0",
"gulp-less": "~1.1.10",
"gulp-notify": "~0.3.4-2",
"gulp-minify-css": "~0.2.0",
"gulp-uglify": "~0.1.0",
"gulp-concat": "~2.1.7",
"gulp-jshint": "~1.3.4",
"gulp-replace": "~0.2.0",
"event-stream": "~3.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment