Skip to content

Instantly share code, notes, and snippets.

@zabirauf
Created October 19, 2015 00:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zabirauf/8be86e3e662f1658c367 to your computer and use it in GitHub Desktop.
Save zabirauf/8be86e3e662f1658c367 to your computer and use it in GitHub Desktop.
Gulpfile for a frontend in React + ES6
var gulp = require('gulp'),
gutil = require('gulp-util'),
babelify = require('babelify'),
source = require('vinyl-source-stream'),
browserify = require('browserify'),
watchify = require('watchify'),
browserSync = require('browser-sync').create();
var paths = {
HTML: 'src/index.html',
ALL: ['src/js/*.js', 'src/js/**/*.js', 'src/index.html'],
JS: ['src/js/*.js', 'src/js/**/*.js'],
OUT: 'build.js',
DEST_BUILD: 'dist/build',
DEST: 'dist',
ENTRY_POINT: __dirname + '/src/js/app.js'
};
gulp.task('default', ['copy-static', 'watch']);
// Copies the static file to build folder
gulp.task('copy-static', function() {
gulp.src(paths.HTML)
.pipe(gulp.dest(paths.DEST));
});
// Build the app
gulp.task('build', function() {
return browserify({
extensions: ['js'],
entries: paths.ENTRY_POINT,
debug: true,
paths: './bower_components'
})
.transform(babelify.configure({
ignore: /(bower_components)|(node_modules)/
}))
.bundle()
.on('error', gutil.log)
.pipe(source(paths.OUT))
.pipe(gulp.dest(paths.DEST_BUILD));
});
// Watch for files and do the build
gulp.task('watch', ['copy-static'], function() {
gulp.watch(paths.ALL, ['copy-static']);
var watcher = watchify(browserify({
entries: paths.ENTRY_POINT,
transform: [babelify],
debug: true,
paths: './bower_components',
cache: {},
packageCache: {}
}));
watcher.on('update', function(){
watcher.bundle()
.on('error', gutil.log)
.pipe(source(paths.OUT))
.pipe(gulp.dest(paths.DEST_BUILD))
.pipe(browserSync.reload({stream: true}));
console.log('Updated');
})
.bundle()
.pipe(source(paths.OUT))
.pipe(gulp.dest(paths.DEST_BUILD));
browserSync.init({
server: {
baseDir: paths.DEST
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment