forked from browserify/watchify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
220 lines (191 loc) · 6.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var through = require('through2');
var path = require('path');
var chokidar = require('chokidar');
var xtend = require('xtend');
var anymatch = require('anymatch');
module.exports = watchify;
module.exports.args = {
cache: {}, packageCache: {}
};
function watchify (b, opts) {
if (!opts) opts = {};
var cache = b._options.cache;
var pkgcache = b._options.packageCache;
var delay = typeof opts.delay === 'number' ? opts.delay : 100;
var changingDeps = {};
var pending = false;
var updating = false;
var wopts = {persistent: true};
if (opts.ignoreWatch) {
var ignored = opts.ignoreWatch !== true
? opts.ignoreWatch
: '**/node_modules/**';
}
if (opts.poll || typeof opts.poll === 'number') {
wopts.usePolling = true;
wopts.interval = opts.poll !== true
? opts.poll
: undefined;
}
if (cache) {
b.on('reset', collect);
collect();
}
function collect () {
b.pipeline.get('deps').push(through.obj(function(row, enc, next) {
var file = row.expose ? b._expose[row.id] : row.file;
cache[file] = {
source: row.source,
deps: xtend(row.deps)
};
this.push(row);
next();
}));
}
b.on('file', function (file) {
watchFile(file);
});
b.on('package', function (pkg) {
var file = path.join(pkg.__dirname, 'package.json');
watchFile(file);
if (pkgcache) pkgcache[file] = pkg;
});
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;
this.push(buf);
next();
}
function end () {
var delta = Date.now() - time;
b.emit('time', delta);
b.emit('bytes', bytes);
b.emit('log', bytes + ' bytes written ('
+ (delta / 1000).toFixed(2) + ' seconds)'
);
this.push(null);
}
}
var fwatchers = {};
var fwatcherFiles = {};
var ignoredFiles = {};
if (opts.entryGlob) {
fwatchers._entryWatcher = [watchEntries(opts.entryGlob)];
}
b.on('transform', function (tr, mfile) {
tr.on('file', function (dep) {
watchFile(mfile, dep);
});
});
b.on('bundle', function (bundle) {
updating = true;
bundle.on('error', onend);
bundle.on('end', onend);
function onend () { updating = false }
});
function watchFile (file, dep) {
dep = dep || file;
if (ignored) {
if (!ignoredFiles.hasOwnProperty(file)) {
ignoredFiles[file] = anymatch(ignored, file);
}
if (ignoredFiles[file]) return;
}
if (!fwatchers[file]) fwatchers[file] = [];
if (!fwatcherFiles[file]) fwatcherFiles[file] = [];
if (fwatcherFiles[file].indexOf(dep) >= 0) return;
var w = b._watcher(dep, wopts);
w.setMaxListeners(0);
w.on('error', b.emit.bind(b, 'error'));
w.on('change', function () {
invalidate(file);
});
fwatchers[file].push(w);
fwatcherFiles[file].push(dep);
}
function invalidate (id) {
if (cache) delete cache[id];
if (pkgcache) delete pkgcache[id];
changingDeps[id] = true;
if (!updating && fwatchers[id]) {
fwatchers[id].forEach(function (w) {
w.close();
});
delete fwatchers[id];
delete fwatcherFiles[id];
}
// wait for the disk/editor to quiet down first:
if (pending) clearTimeout(pending);
pending = setTimeout(notify, delay);
}
function notify () {
if (updating) {
pending = setTimeout(notify, delay);
} else {
pending = false;
b.emit('update', Object.keys(changingDeps));
changingDeps = {};
}
}
function watchEntries(glob) {
var entriesAdded = {};
var entriesRemoved = {};
var basedir = b._options.basedir || process.cwd();
var watchOpts = xtend({ cwd: basedir }, wopts);
watchOpts.ignoreInitial = true;
var watcher = chokidar.watch(glob, watchOpts);
watcher.on('error', b.emit.bind(b, 'error'));
watcher.on('add', function (file) {
file = path.join(basedir, file);
entriesAdded[file] = true;
if (entriesRemoved[file]) {
delete entriesRemoved[file];
}
invalidate(file);
});
watcher.on('unlink', function (file) {
file = path.join(basedir, file);
entriesRemoved[file] = true;
if (entriesAdded[file]) {
delete entriesAdded[file];
}
invalidate(file);
});
b.on('reset', function () {
var added = entriesAdded;
var removed = entriesRemoved;
entriesAdded = {};
entriesRemoved = {};
var entryOrder = 0;
b.pipeline.get('record').unshift(through.obj(function (row, enc, next) {
if (row.file && removed[row.file]) {
return next();
}
if (row.entry) {
// for browser-pack
row.order = entryOrder++;
}
next(null, row);
}));
b.add(Object.keys(added), { basedir: b._options.basedir });
})
return watcher;
}
b.close = function () {
Object.keys(fwatchers).forEach(function (id) {
fwatchers[id].forEach(function (w) { w.close() });
});
};
b._watcher = function (file, opts) {
return chokidar.watch(file, opts);
};
return b;
}