Skip to content

Instantly share code, notes, and snippets.

@L1fescape
Last active August 29, 2015 14:05
Show Gist options
  • Save L1fescape/0569816d325ddc75f4fa to your computer and use it in GitHub Desktop.
Save L1fescape/0569816d325ddc75f4fa to your computer and use it in GitHub Desktop.
Seperate Gulp browserify and watchify tasks
'use strict';
var gulp = require('gulp');
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var notify = require('gulp-notify');
// Helper functions
function handleErrors(){
var args = Array.prototype.slice.call(arguments);
// Send error to notification center with gulp-notify
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
// Keep gulp from hanging on this task
this.emit('end');
}
function bundle(args){
var bundler;
if( args ){
bundler = browserify('./app/javascripts/main.js', args);
} else {
bundler = browserify('./app/javascripts/main.js');
}
bundler.bundle()
.on('error', handleErrors)
.pipe(source('app.js'))
.pipe(gulp.dest('./dist/'));
return bundler;
}
// Tasks
gulp.task('browserify', bundle);
gulp.task('watch', function() {
var bundler = watchify( bundle(watchify.args) );
bundler.on('update', bundle);
return bundler;
});
gulp.task('default', ['watch']);
@L1fescape
Copy link
Author

Note: this still needs to be revised because of a memory leak due to constantly calling browserify. Should just be calling .bundle() instead of browserify each time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment