Skip to content

Instantly share code, notes, and snippets.

@a-double
Created January 8, 2016 16:15
Show Gist options
  • Select an option

  • Save a-double/e1e0eaa0da38932ad37c to your computer and use it in GitHub Desktop.

Select an option

Save a-double/e1e0eaa0da38932ad37c to your computer and use it in GitHub Desktop.
Basic Gulpfile for React/Babelify/Browserify
{
"presets": [ "es2015", "react" ],
"plugins": [ "transform-decorators-legacy" ]
}
var gulp = require( 'gulp' ),
gutil = require( 'gulp-util' ),
notify = require( 'gulp-notify' ),
babelify = require( 'babelify' ),
browserify = require( 'browserify' ),
watchify = require( 'watchify' ),
source = require( 'vinyl-source-stream' ),
historyApiFallback = require( 'connect-history-api-fallback' ),
browserySync = require( 'browser-sync' ),
reload = browserSync.reload;
function handleErrors() {
var args = Array.prototype.slice.call( arguments );
notify.onError({
title: 'Compilation Error',
message: '<%= error.message %>'
}).apply( this, args );
this.emit( 'end' );
}
function buildScript( file, watch ) {
var props = {
entries: './scripts/main.js',
debug: true,
transform: babelify,
presets: [ 'es2015', 'react' ]
};
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/' ) )
.pipe( reload({ stream: true }) );
}
bundler.on( 'update', function() {
rebundle();
gutil.log( 'Rebundle...' );
});
return rebundle();
}
gulp.task( 'browser-sync', function() {
browserSync({
server: {},
middleware: [ historyApiFallback() ],
ghostMode: false
});
});
gulp.task( 'scripts', function() {
return buildScript( 'main.js', false );
});
gulp.task( 'default', [ 'scripts', 'browser-sync' ], function() {
return buildScript( 'main.js', true );
});
{
"name": "project-name",
"version": "0.1.0",
"description": "",
"private": true,
"devDependencies": {
"autobind-decorator": "^1.3.2",
"babel": "^6.3.26",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babelify": "^7.2.0",
"browser-sync": "^2.11.0",
"browserify": "^12.0.1",
"connect-history-api-fallback": "^1.1.0",
"gulp": "^3.9.0",
"gulp-notify": "^2.2.0",
"gulp-util": "^3.0.7",
"react": "^0.14.6",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.6.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment