Skip to content

Instantly share code, notes, and snippets.

@danielhutchinson
Created June 4, 2015 08:18
Show Gist options
  • Save danielhutchinson/104ee795ec3c07b06909 to your computer and use it in GitHub Desktop.
Save danielhutchinson/104ee795ec3c07b06909 to your computer and use it in GitHub Desktop.
Gulp + React + Babel + Browserify
var source = require('vinyl-source-stream');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var babelify = require('babelify');
var watchify = require('watchify');
var notify = require("gulp-notify");
var scriptsDir = './app/scripts';
var buildDir = './app/build';
// Based on: http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
function buildScript(file, watch) {
var props = watchify.args;
props.entries = [scriptsDir + '/' + file];
props.debug = true;
props.extensions = ['.jsx'];
var bundler = watch ? watchify(browserify(props)) : browserify(props);
bundler.transform(babelify);
function rebundle() {
var stream = bundler.bundle();
return stream.on('error', notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}))
.pipe(source('app.js'))
.pipe(gulp.dest(buildDir + '/'));
}
bundler.on('update', function() {
rebundle();
gutil.log('Rebundle...');
});
return rebundle();
}
gulp.task('build', function() {
return buildScript('index.jsx', false);
});
gulp.task('default', ['build'], function() {
return buildScript('index.jsx', true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment