Skip to content

Instantly share code, notes, and snippets.

@SergeyMaas
Forked from campezzi/gulpfile.js
Last active August 29, 2015 14:26
Show Gist options
  • Save SergeyMaas/c90ec3ada73b1e42a7ae to your computer and use it in GitHub Desktop.
Save SergeyMaas/c90ec3ada73b1e42a7ae 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