-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (49 loc) · 1.06 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
var fs = require('fs');
module.exports = function reloadingRequire(filename) {
var proxy;
var rearm;
var _fnImpl;
var rearmObject = function(mod) {
for(i in proxy) {
delete proxy[i];
}
for(i in mod) {
proxy[i] = mod[i];
}
};
var rearmFunction = function(mod) {
_fnImpl = mod;
for(i in proxy.prototype) {
delete proxy.prototype[i];
}
for(i in proxy) {
delete proxy[i];
}
for(i in mod.prototype) {
proxy.prototype[i] = mod.prototype[i];
}
for(i in mod) {
if(mod.hasOwnProperty(i)) {
proxy[i] = mod[i];
}
}
};
var initial = require(filename);
if(typeof initial === 'function') {
proxy = function() {
return _fnImpl.apply(this, arguments);
};
rearm = rearmFunction;
} else {
proxy = {};
rearm = rearmObject;
}
rearm(initial);
var path = require.resolve(filename);
fs.watch(path, function(evtType) {
if(evtType !== 'change') return;
delete require.cache[path];
rearm(require(filename));
});
return proxy;
};