Skip to content

Instantly share code, notes, and snippets.

@stofte
Created June 12, 2015 20:39
Show Gist options
  • Save stofte/ccb19c4b3d58dff96e69 to your computer and use it in GitHub Desktop.
Save stofte/ccb19c4b3d58dff96e69 to your computer and use it in GitHub Desktop.
Gulpfile that serves an app using browserify with babel to continually build es6/harmony/react with map files, continually inject less, using eslint as a linter. Has chrome flag that only transforms stuff Chrome doesn't support yet
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var eslint = require('gulp-eslint');
var gulpIf = require('gulp-if');
var less = require('gulp-less');
var uglify = require('gulp-uglify');
var minifyCss = require('gulp-minify-css');
var sourcemaps = require('gulp-sourcemaps');
var babelify = require('babelify');
var watchify = require('watchify');
var browserify = require('browserify');
var browserSync = require('browser-sync');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var assign = require('object-assign');
// minification, etc
var debug = true;
var chrome = false;
gulp.task('default', ['serve']);
////////////////////////////////////////////////////////////////////////////////
// browserify
var opts = assign({}, watchify.args, {
entries: ['./js/app.js'],
debug: debug
});
var babelOpts = {
sourceMaps: debug
};
if (chrome) {
babelOpts.whitelist = ['react', 'es6.arrowFunctions'];
}
var b = watchify(browserify(opts).transform(babelify.configure(babelOpts)));
gulp.task('js', bundle);
b.on('update', bundle);
b.on('log', gutil.log);
function bundle() {
return b
.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulpIf(!debug, uglify()))
.on('error', gutil.log)
.pipe(gulp.dest('./js'));
}
////////////////////////////////////////////////////////////////////////////////
// linting
gulp.task('lint', function lint() {
return gulp.src(['js/**/*.js', '!js/bundle.js'])
.pipe(eslint())
.pipe(eslint.format());
});
////////////////////////////////////////////////////////////////////////////////
// styles
gulp.task('less', function () {
return gulp.src('./css/bundle.less')
.pipe(sourcemaps.init())
.pipe(less())
.on('error', function swallowError (error) {
//If you want details of the error in the console
console.log(error.toString());
this.emit('end');
})
.pipe(gulpIf(!debug, minifyCss()))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream());
});
////////////////////////////////////////////////////////////////////////////////
// browserSync serve
gulp.task('serve', ['less', 'lint', 'js'], function() {
browserSync({
ghostMode: false,
server: {
baseDir: '.',
index: 'index.html'
}
});
gulp.watch('css/**/*.less', ['less']);
gulp.watch(['js/**/*.js', '!js/bundle.js'], ['lint']);
gulp.watch('js/bundle.js').on('change', browserSync.reload);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment