Created
February 1, 2015 07:30
-
-
Save skw/f87619d8f30e829e73d7 to your computer and use it in GitHub Desktop.
Gulp browserify watchify task(s)
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
"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