-
Notifications
You must be signed in to change notification settings - Fork 33
/
dev-electron.mjs
64 lines (58 loc) · 1.75 KB
/
dev-electron.mjs
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
import * as rollup from 'rollup'
import chokidar from 'chokidar'
// 引入 rollup 配置文件
import rollupConfig from './rollup.config.js'
import { spawn } from 'child_process'
import electron from 'electron'
const httpAddress = 'http://localhost:1234'
let electronProcess
async function build() {
try {
const bundle = await rollup.rollup(rollupConfig)
await bundle.write(rollupConfig.output)
// 创建 electron 进程
createElectronProcess()
} catch (error) {
console.error(error)
}
}
// 创建 electron 进程
async function createElectronProcess() {
electronProcess = await spawn(electron, ['./dist-electron/main.js', httpAddress], {
cwd: process.cwd(),
// stdio: 'inherit',
})
electronProcess.on('close', closeElectronProcess)
electronProcess.on('error', closeElectronProcess)
electronProcess.stdout.on('data', (data) => {
console.log(data.toString())
})
electronProcess.stderr.on('data', (data) => {
console.error(data.toString())
})
}
// 退出进程
function closeElectronProcess(code) {
if (electronProcess && electronProcess.kill) {
console.log(`🤖 electron 进程退出,退出码 ${code}`)
process.kill(electronProcess.pid)
electronProcess = null
}
// 退出进程
// process.exit()
}
// 文件变化时,重新构建
function onFileChange() {
console.log('🏃🏃🏃🏃🏃🏃🏃文件更改,正在重新构建...')
// 退出上一个 electron 进程
closeElectronProcess()
build()
}
// 监听 src中所有文件的变化
const chokidarWatcher = chokidar.watch(['electron/**/*'], {
ignored: /(^|[\/\\])\../, // 忽略以点开头的文件
persistent: true, // 保持监听状态
})
// 当文件变化时,重新构建
chokidarWatcher.on('change', onFileChange)
build()