Skip to content

Instantly share code, notes, and snippets.

@campezzi
Created March 24, 2015 23:20
Show Gist options
  • Save campezzi/8f8285af55bc27add5ae to your computer and use it in GitHub Desktop.
Save campezzi/8f8285af55bc27add5ae to your computer and use it in GitHub Desktop.
Gulp + Browserify + Reactify + Watch + LiveReload
var source = require('vinyl-source-stream');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var reactify = require('reactify');
var notify = require("gulp-notify");
var livereload = require('gulp-livereload');
var scriptsDir = './src/js';
var buildDir = './public/js';
function handleErrors() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
this.emit('end'); // Keep gulp from hanging on this task
}
function buildScript(file) {
var options = {entries: [scriptsDir + '/' + file], debug: true};
var bundler = browserify(options);
bundler.transform(reactify);
function rebundle() {
var stream = bundler.bundle();
return stream.on('error', handleErrors)
.pipe(source(file))
.pipe(gulp.dest(buildDir + '/'))
.pipe(livereload());
}
bundler.on('update', function() {
rebundle();
gutil.log('Rebundle...');
});
return rebundle();
}
gulp.task('build', function() {
buildScript('main.js');
});
gulp.task('watch', function() {
livereload.listen();
gulp.watch(scriptsDir + '/*.js', ['build']);
});
gulp.task('default', ['build', 'watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment