Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #56 #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
56 changes: 39 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function watchify (b, opts) {
var pkgcache = b._options.packageCache;
var changingDeps = {};
var pending = false;

b.on('dep', function (dep) {
if (typeof dep.id === 'string') {
cache[dep.id] = dep;
Expand All @@ -23,25 +23,25 @@ function watchify (b, opts) {
watchFile(dep.file);
}
});

b.on('file', function (file) {
watchFile(file);
});

b.on('package', function (pkg) {
watchFile(path.join(pkg.__dirname, 'package.json'));
});

b.on('reset', reset);
reset();

function reset () {
var time = null;
var bytes = 0;
b.pipeline.get('record').on('end', function () {
time = Date.now();
});

b.pipeline.get('wrap').push(through(write, end));
function write (buf, enc, next) {
bytes += buf.length;
Expand All @@ -58,28 +58,50 @@ function watchify (b, opts) {
this.push(null);
}
}

var fwatchers = {};
var fwatcherFiles = {};


if (opts.glob) {
var w = chokidar.watch(opts.glob, {ignoreInitial: true, persistent: true});
w.setMaxListeners(0);
w.on('error', b.emit.bind(b, 'error'));
w.on('add', function (file) {
b._options.entries.push(file);
b.constructor.call(b, b._options);
invalidate(file);
});
w.on('unlink', function (file) {
var i = b._options.entries.map(function (str) {
return path.resolve(str);
}).indexOf(path.resolve(file));
if (i === -1)
return;
b._options.entries.splice(i, 1);
b.constructor.call(b, b._options);
invalidate(file);
});
fwatchers.glob = w;
}

b.on('transform', function (tr, mfile) {
tr.on('file', function (file) {
watchDepFile(mfile, file);
});
});

function watchFile (file) {
fs.lstat(file, function (err, stat) {
if (err || stat.isDirectory()) return;
watchFile_(file);
});
}

function watchFile_ (file) {
if (!fwatchers[file]) fwatchers[file] = [];
if (!fwatcherFiles[file]) fwatcherFiles[file] = [];
if (fwatcherFiles[file].indexOf(file) >= 0) return;

var w = chokidar.watch(file, {persistent: true});
w.setMaxListeners(0);
w.on('error', b.emit.bind(b, 'error'));
Expand All @@ -89,7 +111,7 @@ function watchify (b, opts) {
fwatchers[file].push(w);
fwatcherFiles[file].push(file);
}

function watchDepFile(mfile, file) {
if (!fwatchers[mfile]) fwatchers[mfile] = [];
if (!fwatcherFiles[mfile]) fwatcherFiles[mfile] = [];
Expand All @@ -104,7 +126,7 @@ function watchify (b, opts) {
fwatchers[mfile].push(w);
fwatcherFiles[mfile].push(file);
}

function invalidate (id) {
if (cache) delete cache[id];
if (fwatchers[id]) {
Expand All @@ -115,22 +137,22 @@ function watchify (b, opts) {
delete fwatcherFiles[id];
}
changingDeps[id] = true

// wait for the disk/editor to quiet down first:
if (!pending) setTimeout(function () {
pending = false;
b.emit('update', Object.keys(changingDeps));
changingDeps = {};

}, opts.delay || 600);
pending = true;
}

b.close = function () {
Object.keys(fwatchers).forEach(function (id) {
fwatchers[id].forEach(function (w) { w.close() });
});
};

return b;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bin": "bin/cmd.js",
"dependencies": {
"browserify": "^9.0.2",
"chokidar": "~0.12.1",
"chokidar": "^1.0.0-rc3",
"through2": "~0.5.1"
},
"devDependencies": {
Expand Down