-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.after.js
139 lines (116 loc) · 4.32 KB
/
webpack.after.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
const child_process = require('child_process')
const getPort = require('get-port')
const websocket = require('ws')
const chalk = require('chalk')
const logging = {
colors: {
types: {
REACT_STDOUT: '#bbadff',
REACT_STDERR: '#ff6a63',
ELECTRON_STDOUT: '#3fbbfe',
ELECTRON_STDERR: '#ff6a63',
},
default: '#dbd9db',
data: '#dbd9db',
time: '#53ff45',
delimiter_open: '#ff9e00',
delimiter_close: '#ff9e00'
},
delimiter: {
open: '',
close: ' | ',
}
}
// const padToMaxTypeLength = (type) => {
// const padding = Object.keys(logging.colors.types).sort((a, b) => b.length - a.length)[0].length - type.length
// return padding > 0 ? type + ' '.repeat(padding) : type
// }
const log = (data, type) => {
const color = logging.colors.types[type] ? logging.colors.types[type] : logging.colors.default
data = data.toString().split('\n')
data.splice(data.length - 1, 1)
data.forEach(_data => {
process.stdout.write(
chalk.hex(logging.colors.delimiter_open)(logging.delimiter.open) +
chalk.hex(logging.colors.time)(new Date().toISOString()) +
chalk.hex(logging.colors.delimiter_close)(logging.delimiter.close) +
chalk.hex(logging.colors.delimiter_open)(logging.delimiter.open) +
// chalk.hex(color)(color === logging.colors.default ? 'UNKNOWN_PROCESS' : padToMaxTypeLength(type)) +
chalk.hex(color)(color === logging.colors.default ? 'UNKNOWN_PROCESS' : type) +
chalk.hex(logging.colors.delimiter_close)(logging.delimiter.close) +
chalk.hex(logging.colors.data)(_data + '\n')
)
})
}
var electronProcess
var electronWs
module.exports = async (app, server, compiler) => {
let electronStarted = false
let emit = false
const port = await getPort({ port: getPort.makeRange(5200, 5500) })
const electronPort = await getPort({ port: getPort.makeRange(5501, 5800) })
const reactProcess = child_process.spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run', 'react-dev'], {
env: {
...process.env,
PORT: port.toString(),
NODE_ENV: 'development',
BROWSER: 'none',
},
})
process.on('exit', () => {
reactProcess && reactProcess.kill()
electronProcess && electronProcess.kill()
child_process.execSync(`npx kill-port ${port}`)
})
reactProcess.on('error', err => {
console.error('[REACT] Error: ' + err.message)
process.exit()
})
reactProcess.on('exit', () => {
console.log('[REACT] Closed, terminating process...')
process.exit()
})
reactProcess.stdout.on('data', data => {
log(data.toString(), 'REACT_STDOUT')
electronStarted || startElectron()
})
reactProcess.stderr.on('data', data => log(data.toString(), 'REACT_STDERR'))
const connectToDevWs = () => {
electronWs = new websocket(`ws://localhost:${electronPort}`)
electronWs.on('error', err => setTimeout(connectToDevWs, 1000))
// electronWs.on('open', () => console.log(`[Dev Websocket Connection Open] ws://localhost:${electronPort}`))
electronWs.on('message', message => console.log('received: %s', message))
}
const restartProcessRunner = () => {
// console.log('Sending restart child process signal.')
electronWs.send('restartProcessRunner')
}
const startElectron = () => {
electronStarted = true
if (emit) {
electronProcess = child_process.spawn(require('electron'), ['./build/main.js'], {
env: {
...process.env,
ELECTRON_START_URL: 'http://localhost:' + port,
DEV_WEBSOCKET_PORT: electronPort,
},
})
electronProcess.on('error', err => {
console.error('[ELECTRON] Error: ' + err.message)
process.exit()
})
electronProcess.on('exit', () => {
console.log('[ELECTRON] Closed, terminating process...')
process.exit()
})
electronProcess.stdout.on('data', data => log(data, 'ELECTRON_STDOUT'))
electronProcess.stderr.on('data', data => log(data, 'ELECTRON_STDERR'))
connectToDevWs()
} else setTimeout(startElectron, 250)
}
compiler.hooks.afterEmit.tapAsync('RunElectron', async (params, callback) => {
emit && electronStarted && restartProcessRunner()
emit = true
return callback()
})
}