Skip to content

Instantly share code, notes, and snippets.

@webfella
Created December 27, 2014 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webfella/fbe5b1ca3bb6573e3327 to your computer and use it in GitHub Desktop.
Save webfella/fbe5b1ca3bb6573e3327 to your computer and use it in GitHub Desktop.
Watchify Gulp Task
var _ = require('lodash'),
browserify = require('browserify'),
buffer = require('vinyl-buffer'),
config = require('../../config'),
gulp = require('gulp'),
gutil = require('gulp-util'),
paths = require('../../helpers/paths'),
rename = require('gulp-rename'),
source = require('vinyl-source-stream'),
transform = require('vinyl-transform'),
watchify = require('watchify');
gulp.task('watchify', function() {
var watchified = transform(function(fileName) {
var b = browserify(fileName, _.extend({debug: true}, watchify.args)),
bundler = watchify(b);
bundler.on('update', rebundle.bind(bundler));
return bundler.bundle();
});
return gulp.src(config.paths.src)
.on('error', gutil.log.bind(gutil, config.messages.error))
.pipe(watchified)
.pipe(rename(paths.fileOutput))
.pipe(gulp.dest(config.paths.dest));
function rebundle() {
var trimmedPath = paths.trimPath(this._bpack.standaloneModule);
return this.bundle()
.on('end', gutil.log.bind(gutil, config.messages.rebuild))
.on('error', gutil.log.bind(gutil, config.messages.error))
.pipe(source(trimmedPath))
.pipe(buffer())
.pipe(rename(paths.fileOutput))
.pipe(gulp.dest(config.paths.dest));
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment