Skip to content

Instantly share code, notes, and snippets.

@wesbos
Last active October 21, 2019 19:38
Show Gist options
  • Save wesbos/52b8fe7e972356e85b43 to your computer and use it in GitHub Desktop.
Save wesbos/52b8fe7e972356e85b43 to your computer and use it in GitHub Desktop.
FAST Browserify + Reactify + Babelify
// Update: Hey Folks - I've got a full Gulpfile with everything else over at https://github.com/wesbos/React-For-Beginners-Starter-Files
var source = require('vinyl-source-stream');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var reactify = require('reactify');
var babelify = require('babelify');
var watchify = require('watchify');
var notify = require('gulp-notify');
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: ['./scripts/' + file],
debug : true,
transform: [babelify, reactify]
};
// 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(file))
.pipe(gulp.dest('./build/'));
}
// 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.js', false);
});
// run 'scripts' task first, then watch for future changes
gulp.task('default', ['scripts'], function() {
return buildScript('app.js', true);
});
@saxinte
Copy link

saxinte commented Aug 17, 2015

+1

@kukagg
Copy link

kukagg commented Sep 10, 2015

great, thx

@igorsvee
Copy link

works like a charm, thanks

@giobyte8
Copy link

Great, Thanks a lot !!

@monojones
Copy link

Great! This code is correctly using new browserify.bundle() without arguments. Arguments must be set on the constructor.
+1

@limi58
Copy link

limi58 commented Oct 12, 2015

great !!!

@rubensdev
Copy link

It would be more faster if it includes these properties in the props object. These properties are required for watchify

cache: {},
packageCache: {},
fullPaths: true

@barbu110
Copy link

Hi!
This takes 7.13 seconds to run on my computer, while

gulp.task('build', function () {
  browserify({
    entries: 'index.js',
    extensions: ['.js'],
    debug: (process.env.NODE_ENV === 'development')
  })
  .transform(babelify)
  .bundle()
  .pipe(source('test1.js'))
  .pipe(buffer())
  .pipe(uglify())
  .pipe(gulp.dest('/var/www/res/dist'));
});

takes 17 ms. Why?

@angkywilliam
Copy link

In case you guys need to integrate Sass and Livereload to your gulpfile
https://github.com/angkywilliam/ReactGulpBoilerPlate/blob/master/gulpfile.js

@Tushant
Copy link

Tushant commented Jan 18, 2016

what should be done for multiple bundling?

@rico345100
Copy link

i just copy and paste this source code but it is not working on me:

Unexpected token (7:1) while parsing file: ...

directories are:
source/app.js
source/lib/math.js

source/lib/math.js:
export const sqrt = Math.sqrt;
export function square(x) {
return x*x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}

source/app.js:
import { square, diag } from './lib/math.js';
console.log(square(11));
console.log(diag(4,3));

//react
ReactDOM.render(
jsx syntax here...,
document.getElementById('app')
);

what is the problem? help will many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment