Skip to content

Instantly share code, notes, and snippets.

@pbaio
Last active April 18, 2017 18:51
Show Gist options
  • Save pbaio/c12eedf6852aa967dd847650bc96cd17 to your computer and use it in GitHub Desktop.
Save pbaio/c12eedf6852aa967dd847650bc96cd17 to your computer and use it in GitHub Desktop.
gulpfile with Browserify, BrowserSync, Nodemon and JSHint, entires values will need to be changed for each application that uses this
'use strict';
const browserify = require('browserify');
const browserSync = require('browser-sync');
const gulp = require('gulp');
const gutil = require('gulp-util');
const nodemon = require('gulp-nodemon');
const uglify = require('gulp-uglify');
const watchify = require('watchify');
const jshint = require('gulp-jshint');
const stylish = require('jshint-stylish');
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
var browserifyOpts = {
entries: ['./app/app.js'],
debug: true
};
function bundle(b) {
console.log(`[gulp][${new Date().toString()}] Updating 'public/js/bundle.js'`);
return b.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('bundle.js'))
.pipe(buffer())
//.pipe(uglify())
.pipe(gulp.dest('./public/js'));
}
gulp.task('build', () => {
bundle(browserify(browserifyOpts));
});
gulp.task('watchify', () => {
let b = watchify(browserify({
cache: {},
packageCache: {},
entries: browserifyOpts.entries
}));
b.on('update', () => {
return bundle(b);
});
return bundle(b);
});
gulp.task('browserSync', ['nodemon'], () => {
browserSync({
files: ['./app/**/*.html', './views/*.html', './public/css/*.css'],
browser: ['google chrome'],
proxy: 'http://localhost:8080',
port: 3000,
online: true,
open: 'local',
reloadDelay: 3000
});
});
gulp.task('nodemon', (cb) => {
let started = false;
return nodemon({
script: 'server.js',
env: { 'NODE_ENV': 'qa' }
})
.on('start', () => {
if (!started) {
started = true;
return cb();
}
})
.on('restart', () => {
browserSync.reload();
});
});
gulp.task('lint', () => {
return gulp.src([
'/app/**/*.js',
'*.js',
'**/*.js'
])
.pipe(jshint({
node: true
}))
.pipe(jshint.reporter(stylish))
.on('error', gutil.log.bind(gutil, 'JSHint Error'));
});
gulp.task('watch', ['watchify']);
gulp.task('start', ['watchify', 'browserSync'], browserSync.reload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment