Created
October 19, 2015 00:11
-
-
Save zabirauf/8be86e3e662f1658c367 to your computer and use it in GitHub Desktop.
Gulpfile for a frontend in React + ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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