Skip to content

Instantly share code, notes, and snippets.

@simonrenoult
Created December 23, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simonrenoult/901db88575e3d23e733c to your computer and use it in GitHub Desktop.
Save simonrenoult/901db88575e3d23e733c to your computer and use it in GitHub Desktop.
Gulpfile with browserify, handlebars, webserver, lint, minification, bower, watch, reload
var gulp = require('gulp'),
del = require('del'),
plugins = require('gulp-load-plugins')(),
bower = require('main-bower-files'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
browserify = require('browserify'),
browserSync = require('browser-sync');
var conf = {
build: './build/',
src: './src/'
};
gulp.task('clean:build', function(next) {
return del(conf.build, next);
});
gulp.task('html', function(){
return gulp.src(conf.src + 'index.html')
.pipe(plugins.size({title: 'html:before'}))
.pipe(plugins.htmlmin({collapseWhitespace: true}))
.pipe(plugins.size({title: 'html:after'}))
.pipe(gulp.dest(conf.build));
});
gulp.task('js:lint', function() {
return gulp.src(conf.src + '**/*.js')
.pipe(plugins.jshint())
.pipe(plugins.jshint.reporter('default'));
});
gulp.task('js:browserify', function() {
return browserify(conf.src + 'js/main.js').bundle()
.pipe(source('app.min.js'))
.pipe(buffer())
.pipe(plugins.size({title: 'js:before'}))
.pipe(plugins.uglify())
.pipe(plugins.size({title: 'js:after'}))
.pipe(gulp.dest(conf.build));
});
gulp.task('js', ['js:lint', 'js:browserify']);
gulp.task('css', function(){
return gulp.src(conf.src + '**/*.css')
.pipe(plugins.csslint())
.pipe(plugins.jshint.reporter())
.pipe(plugins.size({title: 'css:before'}))
.pipe(plugins.cssmin())
.pipe(plugins.concat('app.min.css'))
.pipe(plugins.size({title: 'css:after'}))
.pipe(gulp.dest(conf.build));
});
gulp.task('vendors:js', function() {
return gulp.src(bower())
.pipe(plugins.filter('**/*.js'))
.pipe(plugins.size({title: 'js:vendors:before'}))
.pipe(plugins.uglify())
.pipe(plugins.concat('vendors.min.js'))
.pipe(plugins.size({title: 'js:vendors:after'}))
.pipe(gulp.dest(conf.build));
});
gulp.task('vendors:css', function() {
return gulp.src(bower())
.pipe(plugins.filter('**/*.css'))
.pipe(plugins.size({title: 'css:vendors:before'}))
.pipe(plugins.cssmin())
.pipe(plugins.concat('vendors.min.css'))
.pipe(plugins.size({title: 'css:vendors:after'}))
.pipe(gulp.dest(conf.build));
});
gulp.task('vendors', ['vendors:js', 'vendors:css']);
gulp.task('browser-sync', function() {
return browserSync({
server: {
baseDir: conf.build,
open: false
}
});
});
gulp.task('build', ['html', 'js', 'css', 'vendors']);
gulp.task('serve', ['clean:build'], function() {
gulp.start(['build', 'browser-sync']);
gulp.watch(conf.src + 'css/**/*.css', ['css', browserSync.reload]);
gulp.watch(conf.src + 'js/**/*.js', ['js', browserSync.reload]);
gulp.watch(conf.src + 'index.html', ['html', browserSync.reload]);
});
gulp.task('default', ['build']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment