-
-
Save SergeyMaas/c90ec3ada73b1e42a7ae to your computer and use it in GitHub Desktop.
Gulp + Browserify + Reactify + Watch + LiveReload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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