Last active
August 29, 2015 14:23
-
-
Save wilvandertuin/d83fbcc5deb899f28b8b to your computer and use it in GitHub Desktop.
Browserify/Watchify Gulp task with Underscorify and error logging with Gulp Util
This file contains hidden or 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'); | |
var browserify = require('browserify'); | |
var watchify = require('watchify'); | |
var underscorify = require('node-underscorify'); | |
var source = require('vinyl-source-stream'); | |
var gutil = require('gulp-util'); | |
/** | |
* Writes a Browserify or Watchify bundle to file. | |
*/ | |
var write = function(bundler) { | |
var path = './dist/js'; | |
var filename = 'app.js'; | |
bundler | |
.bundle() | |
.on('error', function(message) { | |
gutil.log(message.toString()); | |
}) | |
.pipe(source(filename)) | |
.pipe(gulp.dest(path)); | |
}; | |
gulp.task('watchify', function() { | |
var options = watchify.args; | |
// Set options for Watchify. | |
options.debug = true; | |
options.ignoreMissing = true; | |
// Create Browserify instance. | |
var bundler = browserify(options); | |
bundler.transform(underscorify); | |
bundler.add('./app/index.js'); | |
// Wrap Browserify instance in a Watcherify instance. | |
bundler = watchify(bundler) | |
.on('update', function() { | |
write(bundler); | |
}); | |
// Write has to be called once before it starts to emit `update` events. | |
write(bundler); | |
// Run a watch to keep the process alive for now. This is just here to keep the process going. | |
// @todo Call the Watchify task as a dependency from another watch task and remove this. | |
gulp.watch('./dist/js/**/*.js', function() { | |
console.log('dist updated'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment