-
Notifications
You must be signed in to change notification settings - Fork 27
/
livereloadServer.js
48 lines (37 loc) · 1.03 KB
/
livereloadServer.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
let tinyLr = require('tiny-lr');
let debounce = require('lodash/debounce');
let config = require('config');
let path = require('path');
let fs = require('fs');
class LivereloadServer {
constructor() {
let options = {
livereload: require.resolve('livereload-js/dist/livereload.js'),
};
if (process.env.HTTPS) {
Object.assign(options, {
key: fs.readFileSync(path.join(config.certDir, 'local-key.pem')),
cert: fs.readFileSync(path.join(config.certDir, 'local.pem')),
});
}
this.tinyLrServer = new tinyLr.Server(options);
this.tinyLrServer.listen(35729, undefined, () => {
console.log("Livereload server listening");
});
this.changes = new Set();
this.flushDebounced = debounce(this.flush, 200);
}
queueFlush(path) {
this.changes.add(path);
this.flushDebounced();
}
flush() {
this.tinyLrServer.changed({
body: {
files: Array.from(this.changes)
}
});
this.changes.clear();
}
}
module.exports = new LivereloadServer();