This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.ts
149 lines (126 loc) · 3.79 KB
/
index.ts
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
140
141
142
143
144
145
146
147
148
149
import { IApi, utils } from 'umi';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { debounce, getDevBuildDir, getMainSrc, getPreloadSrc, logProcess, logProcessErrorOutput } from '../utils';
import path from 'path';
import chalk from 'chalk';
import { build as viteBuild } from 'vite';
import { build as webpackBuild, getWebpackConfig } from './webpack';
import * as fse from 'fs-extra';
import { ElectronBuilder } from '../types';
import { getViteConfig } from './vite';
const { chokidar } = utils;
const electronPath = require('electron');
const TIMEOUT = 500;
/**
* 以开发模式运行
* @param api
*/
export const runDev = async (api: IApi) => {
const { buildType } = api.config
.electronBuilder as ElectronBuilder;
let spawnProcess: ChildProcessWithoutNullStreams | null = null;
const runMain = debounce(() => {
if (spawnProcess !== null) {
spawnProcess.kill('SIGINT');
spawnProcess = null;
}
spawnProcess = spawn(String(electronPath), [path.join(getDevBuildDir(api), 'main.js')]);
spawnProcess.stdout.on('data', d => logProcess('Electron', d.toString(), chalk.blue));
logProcessErrorOutput('Electron', spawnProcess);
spawnProcess.on('close', (code, signal) => {
if (signal != 'SIGINT') {
process.exit(-1);
}
});
return spawnProcess;
}, TIMEOUT);
const buildMain = () => {
if (buildType === 'webpack') {
return webpackBuild(getWebpackConfig(api, 'main'));
} else {
return viteBuild(getViteConfig(api, 'main'));
}
};
const buildMainDebounced = debounce(buildMain, TIMEOUT);
const buildPreload = (): Promise<any> => {
//preload目录存在才编译
if (fse.pathExistsSync(getPreloadSrc(api))) {
if (buildType === 'webpack') {
return webpackBuild(getWebpackConfig(api, 'preload'));
} else {
return viteBuild(getViteConfig(api, 'preload'));
}
}
return Promise.resolve();
};
const buildPreloadDebounced = debounce(buildPreload, TIMEOUT);
const runPreload = debounce(() => {
api.getServer().sockets.forEach(socket => {
socket.write(JSON.stringify({
type: 'ok',
data: {
reload: true,
},
}));
});
}, TIMEOUT);
await Promise.all([
buildMain(),
buildPreload(),
]);
const watcher = chokidar.watch([
`${getMainSrc(api)}/**`,
`${getPreloadSrc(api)}/**`,
`${getDevBuildDir(api)}/**`,
], { ignoreInitial: true });
watcher
.on('unlink', path => {
if (spawnProcess !== null && path.includes(getDevBuildDir(api))) {
spawnProcess.kill('SIGINT');
spawnProcess = null;
}
})
.on('add', path => {
if (path.includes(getDevBuildDir(api))) {
return runMain();
}
if (spawnProcess !== undefined && path.includes('preload.js')) {
return runPreload();
}
})
.on('change', (path) => {
if (path.includes(getMainSrc(api))) {
return buildMainDebounced();
}
if (path.includes('main.js')) {
return runMain();
}
if (path.includes(getPreloadSrc(api))) {
return buildPreloadDebounced();
}
if (path.includes('preload.js')) {
return runPreload();
}
});
await runMain();
};
/**
* 打包
* @param api
*/
export const runBuild = async (api: IApi) => {
const { buildType } = api.config
.electronBuilder as ElectronBuilder;
const preloadSrc = getPreloadSrc(api);
if (buildType === 'webpack') {
await webpackBuild(getWebpackConfig(api, 'main'));
if (fse.pathExistsSync(preloadSrc)) {
await webpackBuild(getWebpackConfig(api, 'preload'));
}
} else {
await viteBuild(getViteConfig(api, 'main'));
if (fse.pathExistsSync(preloadSrc)) {
await viteBuild(getViteConfig(api, 'preload'));
}
}
};