Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"node": true
}
19 changes: 19 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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']);
});
75 changes: 42 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
return taskCallback(plugin);
};
};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}

}