-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (79 loc) · 2.83 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
const net = require('net')
const getPort = require('get-port');
const path = require('path')
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const DefinePlugin = require('webpack/lib/DefinePlugin');
const spawn = require('cross-spawn');
module.exports = class NwJSPlugin {
constructor({ command, commandDir, args }) {
this.command = command || 'nw';
this.commandDir = commandDir;
this.args = args || []
this.portPromise = getPort();
this.sockets = [];
this.portPromise.then((port) => {
net.createServer((socket) => {
this.sockets.push(socket);
if (this.lastErrorMessage) {
socket.write(JSON.stringify(this.lastErrorMessage))
}
socket.on('close', () => {
this.sockets = this.sockets.filter(s => s != socket)
})
})
.listen(port)
.on('error', console.error)
})
}
runNW() {
if (this.nw_instance && this.nw_instance.pid) return;
this.nw_instance = spawn(this.command, [
this.commandDir,
...this.args
], {
stdio: "inherit"
});
}
write(message) {
this.sockets.forEach(socket => socket.write(JSON.stringify(message)))
if (message.status === 'ERROR') {
this.lastErrorMessage = message;
} else {
this.lastErrorMessage = null;
}
}
apply(compiler) {
const definePort = (port) => {
new DefinePlugin({ 'NwJSPlugin_PORT': port }).apply(compiler);
return true;
}
compiler.hooks.watchRun.tapPromise('NwJSPlugin', c => this.portPromise.then(definePort));
compiler.hooks.run.tapPromise('NwJSPlugin', c => this.portPromise.then(definePort));
compiler.hooks.entryOption.tap('NwJSPlugin', () => {
new SingleEntryPlugin(__dirname, path.resolve(__dirname, 'reloader.js'), 'reloader').apply(compiler)
})
compiler.hooks.watchRun.tap('NwJSPlugin', () => {
this.write({
status: "WATCH_RUN"
})
});
compiler.hooks.done.tap('NwJSPlugin', (stats) => {
this.commandDir = this.commandDir || compiler.outputPath
this.runNW();
if (stats.compilation.errors.length) {
this.write({
status: "ERROR",
errors: stats.compilation.errors.map(err => ({
file: err.module.id,
location: err.loc,
toEval: err.module._source._value,
}))
})
} else {
this.write({
status: "DONE",
})
}
});
}
}