Skip to content

Instantly share code, notes, and snippets.

@TedAvery
Forked from wesbos/gulpfile.js
Last active August 30, 2015 07:03
Show Gist options
  • Save TedAvery/54b9901557bcf630c6fb to your computer and use it in GitHub Desktop.
Save TedAvery/54b9901557bcf630c6fb to your computer and use it in GitHub Desktop.
FAST Browserify + Reactify + Babelify + Uglify + Sourcemaps
var babelify = require('babelify');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var gulp = require('gulp');
var gutil = require('gulp-util');
var notify = require('gulp-notify');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var watchify = require('watchify');
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, watch) {
var props = {
entries: ['./' + file],
extensions: ['.jsx'],
debug : true,
transform: [babelify]
};
// watchify() if watch requested, otherwise run browserify() once
var bundler = watch ? watchify(browserify(props)) : browserify(props);
function rebundle() {
var stream = bundler.bundle();
return stream
.on('error', handleErrors)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'));
}
// listen for an update and run rebundle
bundler.on('update', function() {
rebundle();
gutil.log('Rebundle...');
});
// run it once the first time buildScript is called
return rebundle();
}
// run once
gulp.task('scripts', function() {
return buildScript('app.jsx', false);
});
// run 'scripts' task first, then watch for future changes
gulp.task('default', ['scripts'], function() {
return buildScript('app.jsx', true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment