Skip to content

Instantly share code, notes, and snippets.

@skw
Created February 1, 2015 07:30
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 skw/f87619d8f30e829e73d7 to your computer and use it in GitHub Desktop.
Save skw/f87619d8f30e829e73d7 to your computer and use it in GitHub Desktop.
Gulp browserify watchify task(s)
"use strict";
var gulp = require('gulp');
// plugins
var plumber = require('gulp-plumber');
var uglify = require('gulp-uglify');
var notify = require('gulp-notify');
var sourcemaps = require('gulp-sourcemaps');
// deps
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var watchify = require('watchify');
var browserify = require('browserify');
// utils
var handleErrors = require('../util/handleErrors');
var onSuccess = require('../util/onSuccess');
var bundler;
var buildOnce = false;
var b = browserify({
cache: {},
packageCache: {},
fullPaths: true,
extensions: ['.js'],
debug: true
});
var browserifyTask = function() {
buildOnce = true;
bundler = b;
bundler.add('./src/js/main.js');
return rebundle();
};
var watchifyTask = function() {
bundler = watchify(b);
bundler.on('update', rebundle);
bundler.add('./src/js/main.js');
return rebundle();
};
function rebundle() {
return bundler.bundle()
.on('error', handleErrors)
.pipe(plumber())
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(uglify({
mangle: false
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/js/'))
.pipe(notify(onSuccess));
}
gulp.task('browserify', browserifyTask);
gulp.task('watchify', watchifyTask);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment