Skip to content

Instantly share code, notes, and snippets.

@david-j-davis
Created March 26, 2017 18:03
Show Gist options
  • Save david-j-davis/08056c10b2aedbe55b8d1dabd84697e5 to your computer and use it in GitHub Desktop.
Save david-j-davis/08056c10b2aedbe55b8d1dabd84697e5 to your computer and use it in GitHub Desktop.
Gulpfile.js boilerplate
/*!
* gulp
* $ npm install gulp-ruby-sass gulp-autoprefixer gulp-minify-css gulp-jshint gulp-concat gulp-uglify gulp-imagemin gulp-notify gulp-rename gulp-livereload gulp-cache del --save-dev
*/
// Load plugins
var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
del = require('del');
// Styles
gulp.task('styles', function () {
return sass('styles/style.scss', {
style: 'expanded'
})
.pipe(autoprefixer('last 2 version'))
.pipe(gulp.dest('dist/styles'))
.pipe(rename({
suffix: '.min'
}))
.pipe(minifycss())
.pipe(gulp.dest('dist/styles'))
.pipe(notify({
message: 'Styles task complete'
}));
});
// Scripts
gulp.task('scripts', function () {
return gulp.src('scripts/**/*.js')
// .pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('main.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(rename({
suffix: '.min'
}))
.pipe(uglify())
.pipe(gulp.dest('dist/scripts'))
.pipe(notify({
message: 'Scripts task complete'
}));
});
// Images
gulp.task('images', function () {
return gulp.src('images/**/*')
.pipe(cache(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
})))
.pipe(gulp.dest('dist/images'))
.pipe(notify({
message: 'Images task complete'
}));
});
// Clean
gulp.task('clean', function (cb) {
del(['dist/assets/css', 'dist/assets/js', 'dist/assets/img'], cb)
});
// Watch
gulp.task('watch', function () {
// Watch .scss files
gulp.watch('styles/**/*.scss', ['styles']);
// Watch .js files
gulp.watch('scripts/**/*.js', ['scripts']);
// Watch image files
gulp.watch('images/**/*', ['images']);
// Create LiveReload server
livereload.listen();
// Watch any files in dist/, reload on change
gulp.watch(['dist/**']).on('change', livereload.changed);
});
// Default task
gulp.task('default', ['clean', 'watch'], function () {
gulp.start('styles', 'scripts', 'images');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment