Instantly share code, notes, and snippets.

Embed
What would you like to do?
// include gulp
var gulp = require('gulp');
// include plugins
var $ = require('gulp-load-plugins')();
var fs = require('fs');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
// Stream bower dependecies
gulp.task('bower', function () {
gulp.src([
'./src/index.html'
])
.pipe($wiredep({
directory: './bower_components'
}))
.pipe(gulp.dest('./src'));
});
// Compile expanded Sass and minify
gulp.task('styles', function() {
gulp.src('./src/scss/**/*.scss')
.pipe($.plumber())
.pipe($.sass({ style: 'expanded' }))
.pipe($.autoprefixer(['last 8 versions'], { cascade: true }))
.pipe($.rename({ basename: 'app' }))
.pipe(gulp.dest('./dist/css'))
.pipe($.rename({ suffix: '.min' }))
.pipe($.minifyCss())
.pipe(gulp.dest('./dist/css'))
.pipe(reload({ stream: true } ));
});
// Lint JS
gulp.task('lint', function() {
gulp.src([
'./src/js/**/*.js'
])
.pipe($.jshint())
.pipe($.jshint.reporter('default'));
});
// Concat and compress JS
gulp.task('scripts', ['lint'], function() {
gulp.src([
'./src/js/**/*.js'
])
.pipe($.concat('app.js'))
.pipe(gulp.dest('./dist/js'))
.pipe($.rename({ suffix: '.min' }))
.pipe($.uglify())
.pipe(gulp.dest('./dist/js'));
});
// Minify HTML
gulp.task('html', function() {
gulp.src([
'./src/**/*.html'
])
.pipe($.plumber())
.pipe($.pipemin({
js: [$.uglify()]
}))
.pipe($.minifyHtml({ condtionals: true, empty: true, quotes: true, spare: true }))
.pipe(gulp.dest('./dist'));
});
// Compress images
gulp.task('images', function() {
gulp.src('./src/img/**/*')
.pipe($.imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
.pipe(gulp.dest('./dist/img'));
});
// Misc files
gulp.task('misc', function() {
gulp.src([
'./src/.htaccess',
'./src/crossdomain.xml',
'./src/robots.txt',
'./src/sitemap.xml'
])
.pipe(gulp.dest('./dist'));
});
// Browser sync
gulp.task('browser-sync', function() {
browserSync.init({
startPath: './dist/index.html',
server: {
baseDir: './',
index: 'index.html'
}
});
});
// Size
gulp.task('size', function() {
gulp.src(['./dist/index.html', './dist/css/*.css', './dist/js/*.js'])
.pipe($.size());
});
// Reload browsers
gulp.task('reload', function() {
browserSync.reload();
});
// Build files
gulp.task('build', ['styles', 'scripts', 'html', 'images', 'misc', 'size']);
gulp.task('default', ['build', 'browser-sync'], function() {
gulp.watch('./src/scss/**/*.scss', ['styles'])
gulp.watch('./src/js/**/*.js', ['scripts', 'reload'])
gulp.watch('./src/**/*.html', ['html', 'reload'])
gulp.watch('./src/img/**/*', ['images', 'reload']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment