From ea5595c1a02bef052f57b5f09316e09ead34713f Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Fri, 11 Jul 2014 13:47:54 -0600 Subject: [PATCH 1/3] Added error logging --- index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 2195d65..3ba3414 100644 --- a/index.js +++ b/index.js @@ -14,12 +14,16 @@ module.exports = function(taskCallback) { } var bundle if (opt.watch !== false) { - bundle = watchify(opt) - cache[path] = bundle + bundle = watchify(opt); + cache[path] = bundle; bundle.on('update', function() { bundle.updateStatus = 'updated' taskCallback(plugin) - }) + }); + bundle.on('error', function(err) { + gutil.log(err); + throw err; + }); } else { bundle = watchify.browserify(opt) } @@ -50,6 +54,9 @@ module.exports = function(taskCallback) { file = file.clone() delete bundle.updateStatus file.contents = bundle.bundle(opt) + file.contents.on('error', function(err) { + gutil.log(err); + }); // Wait until done or else streamify(uglify()) fails due to buffering file.contents.on('end', callback) this.push(file) @@ -62,4 +69,4 @@ module.exports = function(taskCallback) { return function() { return taskCallback(plugin) } -} \ No newline at end of file +} From 39424b8c1b60f02dbd03b298d7042d1026a3fe68 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 17 Jul 2014 12:19:24 -0600 Subject: [PATCH 2/3] Added gulp task to lint --- .jshintrc | 3 +++ gulpfile.js | 19 +++++++++++++++++ index.js | 60 ++++++++++++++++++++++++++-------------------------- package.json | 7 +++++- 4 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 .jshintrc create mode 100644 gulpfile.js diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..c1f2978 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + "node": true +} diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..686c81f --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,19 @@ +'use strict'; + +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var plumber = require('gulp-plumber'); + +gulp.task('lint', function () { + var stream = gulp.src(['./*.js', '!./node_modules/**']) + .pipe(plumber()) + .pipe(jshint()) + .pipe(jshint.reporter('default')) + .pipe(jshint.reporter('fail')); +}); + +gulp.task('build', ['lint']); + +gulp.task('watch', ['lint'], function () { + gulp.watch('**/*.js', ['scripts']); +}); diff --git a/index.js b/index.js index 3ba3414..413891e 100644 --- a/index.js +++ b/index.js @@ -1,72 +1,72 @@ -var gutil = require('gulp-util') -var merge = require('deepmerge') -var through = require('through2') -var watchify = require('watchify') +var gutil = require('gulp-util'); +var merge = require('deepmerge'); +var through = require('through2'); +var watchify = require('watchify'); -var cache = {} +var cache = {}; module.exports = function(taskCallback) { function getBundle(file, opt) { - var path = file.path + var path = file.path; if (cache[path]) { - return cache[path] + return cache[path]; } - var bundle + var bundle; if (opt.watch !== false) { bundle = watchify(opt); cache[path] = bundle; bundle.on('update', function() { - bundle.updateStatus = 'updated' - taskCallback(plugin) + bundle.updateStatus = 'updated'; + taskCallback(plugin); }); bundle.on('error', function(err) { gutil.log(err); throw err; }); } else { - bundle = watchify.browserify(opt) + bundle = watchify.browserify(opt); } - bundle.updateStatus = 'first' + bundle.updateStatus = 'first'; if (opt.setup) { - opt.setup(bundle) + opt.setup(bundle); } - return bundle + return bundle; } function plugin(opt) { return through.obj(function(file, enc, callback){ if (file.isNull()) { - this.push(file) // Do nothing if no contents - return callback() + this.push(file); // Do nothing if no contents + return callback(); } if (file.isStream()) { - return callback(new Error('gulp-watchify ignores streams')) + return callback(new Error('gulp-watchify ignores streams')); } - var options = merge(opt, { entries:'./'+file.relative, basedir:file.base }) - var bundle = getBundle(file, options) + var options = merge(opt, { entries:'./'+file.relative, basedir:file.base }); + var bundle = getBundle(file, options); if (bundle.updateStatus) { gutil.log( bundle.updateStatus === 'first' ? "Bundling" : "Rebundling", gutil.colors.magenta(file.relative), opt.watch !== false ? '(watch mode)':'' - ) - file = file.clone() - delete bundle.updateStatus - file.contents = bundle.bundle(opt) + ); + file = file.clone(); + delete bundle.updateStatus; + file.contents = bundle.bundle(opt); file.contents.on('error', function(err) { gutil.log(err); }); // Wait until done or else streamify(uglify()) fails due to buffering - file.contents.on('end', callback) - this.push(file) + file.contents.on('end', callback); + this.push(file); } else { - callback() + callback(); } - }) + }); } // Return wrapped Task return function() { - return taskCallback(plugin) - } -} + return taskCallback(plugin); + }; +}; diff --git a/package.json b/package.json index 9024033..09d401c 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "integrates watchify with gulp.js", "main": "index.js", "scripts": { + "prepublish": "./node_modules/.bin/gulp build", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -30,6 +31,10 @@ }, "peerDependencies": { "watchify": "^0.6.1" + }, + "devDependencies": { + "gulp": "^3.8.6", + "gulp-jshint": "^1.7.1", + "gulp-plumber": "^0.6.3" } - } From c00bf40e09bf4f2727786255fe00cec0d1cfb588 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 17 Jul 2014 12:24:01 -0600 Subject: [PATCH 3/3] Using gulp-util PluginError --- index.js | 6 ++++-- package.json | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 413891e..14584c0 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ var merge = require('deepmerge'); var through = require('through2'); var watchify = require('watchify'); +var PLUGIN_NAME = 'watchify'; + var cache = {}; module.exports = function(taskCallback) { @@ -21,7 +23,7 @@ module.exports = function(taskCallback) { taskCallback(plugin); }); bundle.on('error', function(err) { - gutil.log(err); + gutil.log(new gutil.PluginError(PLUGIN_NAME, err)); throw err; }); } else { @@ -55,7 +57,7 @@ module.exports = function(taskCallback) { delete bundle.updateStatus; file.contents = bundle.bundle(opt); file.contents.on('error', function(err) { - gutil.log(err); + gutil.log(new gutil.PluginError(PLUGIN_NAME, err).toString()); }); // Wait until done or else streamify(uglify()) fails due to buffering file.contents.on('end', callback); diff --git a/package.json b/package.json index 09d401c..2a2d6eb 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,8 @@ }, "homepage": "https://github.com/marcello3d/gulp-watchify", "dependencies": { - "gulp-util": "^2.2.14", "deepmerge": "^0.2.7", + "gulp-util": "^3.0.0", "through2": "^0.4.1" }, "peerDependencies": {