-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension-reloader.js
76 lines (74 loc) · 2.25 KB
/
extension-reloader.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
const red = '\u001b[31m';
const reset = '\u001b[0m';
const green = '\u001b[32m';
const WebSocket = require('ws');
class ExtensionReloader {
constructor() {
// create server to tell clients when to reload
this.wss = new WebSocket.Server({port: 8000});
this.wss.on('connection', (ws) => {
ws.on('error', (event) => {
consle.log(red + 'ExtensionReloader error:' + event + reset);
});
ws.on('message', (data) => {
if (data == 'options') {
this.openOptionsPage = true;
} else if (data == 'reloaded') {
if (this.openOptionsPage) {
ws.send('options');
this.openOptionsPage = false;
}
} else {
console.log(data);
}
});
});
this.openOptionsPage = false;
};
apply(compiler) {
compiler.hooks.done.tap('ExtensionReloader', () => {
// count to divide "reload" and "restart"
let clients_count = 0;
let clients_len = this.wss.clients.size;
// send "reload" or "restart"
this.wss.clients.forEach((client) => {
clients_count++;
if (clients_count == clients_len) {
// send "restart" to the last client to reload tab and extension
client.send('restart');
} else {
// send "reload" to reload tab
client.send('reload');
}
})
console.log('reloaded!');
})
// register file to reload with webpack entry
let entry = compiler.options.entry;
let content_reload = './content_reload.js';
if (entry.content) {
if (Array.isArray(entry.content)) {
entry.content.push(content_reload);
} else {
entry.content = [entry.content, content_reload];
}
}
let options_reload = './options_reload.js';
if (entry.options) {
if (Array.isArray(entry.options)) {
entry.options.push(options_reload);
} else {
entry.options = [entry.options, options_reload];
}
}
let background_reload = './background_reload.js';
if (entry.background) {
if (Array.isArray(entry.background)) {
entry.background.push(background_reload);
} else {
entry.background = [entry.background, background_reload];
}
}
};
}
module.exports = ExtensionReloader