-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathindex.ts
More file actions
207 lines (186 loc) · 6.35 KB
/
index.ts
File metadata and controls
207 lines (186 loc) · 6.35 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
type Plugin,
type ConfigEnv,
type UserConfig,
build as viteBuild,
} from 'vite'
import {
resolveServerUrl,
resolveViteConfig,
resolveInput,
mockIndexHtml,
withExternalBuiltins,
treeKillSync,
} from './utils'
// public utils
export {
resolveViteConfig,
withExternalBuiltins,
}
export interface ElectronOptions {
/**
* Shortcut of `build.lib.entry`
*/
entry?: import('vite').LibraryOptions['entry']
vite?: import('vite').InlineConfig
/**
* Triggered when Vite is built every time -- `vite serve` command only.
*
* If this `onstart` is passed, Electron App will not start automatically.
* However, you can start Electroo App via `startup` function.
*/
onstart?: (args: {
/**
* Electron App startup function.
* It will mount the Electron App child-process to `process.electronApp`.
* @param argv default value `['.', '--no-sandbox']`
* @param options options for `child_process.spawn`
* @param customElectronPkg custom electron package name (default: 'electron')
*/
startup: (argv?: string[], options?: import('node:child_process').SpawnOptions, customElectronPkg?: string) => Promise<void>
/** Reload Electron-Renderer */
reload: () => void
}) => void | Promise<void>
}
export function build(options: ElectronOptions) {
return viteBuild(withExternalBuiltins(resolveViteConfig(options)))
}
export default function electron(options: ElectronOptions | ElectronOptions[]): Plugin[] {
const optionsArray = Array.isArray(options) ? options : [options]
let userConfig: UserConfig
let configEnv: ConfigEnv
let mockdInput: Awaited<ReturnType<typeof mockIndexHtml>> | undefined
return [
{
name: 'vite-plugin-electron',
apply: 'serve',
configureServer(server) {
server.httpServer?.once('listening', () => {
Object.assign(process.env, {
VITE_DEV_SERVER_URL: resolveServerUrl(server),
})
const entryCount = optionsArray.length
let closeBundleCount = 0
for (const options of optionsArray) {
options.vite ??= {}
options.vite.mode ??= server.config.mode
options.vite.root ??= server.config.root
options.vite.envDir ??= server.config.envDir
options.vite.envPrefix ??= server.config.envPrefix
options.vite.build ??= {}
if (!Object.keys(options.vite.build).includes('watch')) { // #252
options.vite.build.watch = {}
}
options.vite.build.minify ??= false
options.vite.plugins ??= []
options.vite.plugins.push(
{
name: ':startup',
closeBundle() {
if (++closeBundleCount < entryCount) return
if (options.onstart) {
options.onstart.call(this, {
startup,
// Why not use Vite's built-in `/@vite/client` to implement Hot reload?
// Because Vite only inserts `/@vite/client` into the `*.html` entry file, the preload scripts are usually a `*.js` file.
// @see - https://github.com/vitejs/vite/blob/v5.2.11/packages/vite/src/node/server/middlewares/indexHtml.ts#L399
reload() {
if (process.electronApp) {
(server.hot || server.ws).send({ type: 'full-reload' })
// For Electron apps that don't need to use the renderer process.
startup.send('electron-vite&type=hot-reload')
} else {
startup()
}
},
})
} else {
startup()
}
},
},
)
build(options)
}
})
},
},
{
name: 'vite-plugin-electron',
apply: 'build',
config(config, env) {
userConfig = config
configEnv = env
// Make sure that Electron can be loaded into the local file using `loadFile` after packaging.
config.base ??= './'
},
async configResolved(config) {
const input = resolveInput(config)
if (input == null) {
mockdInput = await mockIndexHtml(config)
}
},
async closeBundle() {
mockdInput?.remove()
for (const options of optionsArray) {
options.vite ??= {}
options.vite.mode ??= configEnv.mode
options.vite.root ??= userConfig.root
options.vite.envDir ??= userConfig.envDir
options.vite.envPrefix ??= userConfig.envPrefix
await build(options)
}
}
},
]
}
/**
* Electron App startup function.
* It will mount the Electron App child-process to `process.electronApp`.
* @param argv default value `['.', '--no-sandbox']`
* @param options options for `child_process.spawn`
* @param customElectronPkg custom electron package name (default: 'electron')
*/
export async function startup(
argv = ['.', '--no-sandbox'],
options?: import('node:child_process').SpawnOptions,
customElectronPkg?: string,
) {
const { spawn } = await import('node:child_process')
// @ts-ignore
const electron = await import(customElectronPkg ?? 'electron')
const electronPath = <any>(electron.default ?? electron)
await startup.exit()
// Start Electron.app
const stdio =
process.platform === 'linux'
// reserve file descriptor 3 for Chromium; put Node IPC on file descriptor 4
? ['inherit', 'inherit', 'inherit', 'ignore', 'ipc']
: ['inherit', 'inherit', 'inherit', 'ipc']
process.electronApp = spawn(electronPath, argv, {
stdio,
...options,
})
// Exit command after Electron.app exits
process.electronApp.once('exit', process.exit)
if (!startup.hookedProcessExit) {
startup.hookedProcessExit = true
process.once('exit', startup.exit)
}
}
startup.send = (message: string) => {
if (process.electronApp) {
// Based on { stdio: [,,, 'ipc'] }
process.electronApp.send?.(message)
}
}
startup.hookedProcessExit = false
startup.exit = async () => {
if (process.electronApp) {
await new Promise((resolve) => {
process.electronApp.removeAllListeners()
process.electronApp.once('exit', resolve)
treeKillSync(process.electronApp.pid!)
})
}
}