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 2195d65..14584c0 100644 --- a/index.js +++ b/index.js @@ -1,65 +1,74 @@ -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 PLUGIN_NAME = 'watchify'; + +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 = 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(new gutil.PluginError(PLUGIN_NAME, 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(new gutil.PluginError(PLUGIN_NAME, err).toString()); + }); // 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) - } -} \ No newline at end of file + return taskCallback(plugin); + }; +}; diff --git a/package.json b/package.json index 9024033..2a2d6eb 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": { @@ -24,12 +25,16 @@ }, "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": { "watchify": "^0.6.1" + }, + "devDependencies": { + "gulp": "^3.8.6", + "gulp-jshint": "^1.7.1", + "gulp-plumber": "^0.6.3" } - }