-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.mjs
104 lines (93 loc) · 2.91 KB
/
watch.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
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
#!/usr/bin/env node
import {build, createServer} from 'vite';
import electronPath from 'electron';
import {spawn} from 'child_process';
/** @type 'production' | 'development'' */
const mode = (process.env.MODE = process.env.MODE || 'development');
/** @type {import('vite').LogLevel} */
const logLevel = 'warn';
/**
* Setup watcher for `main` package
* On file changed it totally re-launch electron app.
* @param {import('vite').ViteDevServer} watchServer Renderer watch server instance.
* Needs to set up `VITE_DEV_SERVER_URL` environment variable from {@link import('vite').ViteDevServer.resolvedUrls}
*/
function setupMainPackageWatcher({resolvedUrls}) {
process.env.VITE_DEV_SERVER_URL = resolvedUrls.local[0];
/** @type {ChildProcess | null} */
let electronApp = null;
return build({
mode,
logLevel,
configFile: 'packages/main/vite.config.ts',
build: {
/**
* Set to {} to enable rollup watcher
* @see https://vitejs.dev/config/build-options.html#build-watch
*/
watch: {},
},
plugins: [
{
name: 'reload-app-on-main-package-change',
writeBundle() {
/** Kill electron if process already exist */
if (electronApp !== null) {
electronApp.removeListener('exit', process.exit);
electronApp.kill('SIGINT');
electronApp = null;
}
/** Spawn new electron process */
electronApp = spawn(String(electronPath), ['--inspect', '.'], {
stdio: 'inherit',
});
/** Stops the watch script when the application has been quit */
electronApp.addListener('exit', process.exit);
},
},
],
});
}
/**
* Setup watcher for `preload` package
* On file changed it reload web page.
* @param {import('vite').ViteDevServer} watchServer Renderer watch server instance.
* Required to access the web socket of the page. By sending the `full-reload` command to the socket, it reloads the web page.
*/
function setupPreloadPackageWatcher({ws}) {
return build({
mode,
logLevel,
configFile: 'packages/preload/vite.config.ts',
build: {
/**
* Set to {} to enable rollup watcher
* @see https://vitejs.dev/config/build-options.html#build-watch
*/
watch: {},
},
plugins: [
{
name: 'reload-page-on-preload-package-change',
writeBundle() {
ws.send({
type: 'full-reload',
});
},
},
],
});
}
/**
* Dev server for Renderer package
* This must be the first,
* because the {@link setupMainPackageWatcher} and {@link setupPreloadPackageWatcher}
* depend on the dev server properties
*/
const rendererWatchServer = await createServer({
mode,
logLevel,
configFile: 'packages/renderer/vite.config.ts',
}).then(s => s.listen());
await setupPreloadPackageWatcher(rendererWatchServer);
await setupMainPackageWatcher(rendererWatchServer);