Skip to content

Commit b361d2a

Browse files
committed
refactor!: vite-plugin-electron instead vite-electron-plugin
1 parent 1fd0096 commit b361d2a

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
"engines": {
1414
"node": "^14.18.0 || >=16.0.0"
1515
},
16+
"dependencies": {},
1617
"devDependencies": {
1718
"@vitejs/plugin-vue": "^3.1.2",
1819
"electron": "^21.1.0",
1920
"electron-builder": "^23.3.3",
2021
"typescript": "^4.8.4",
2122
"vite": "^3.2.2",
22-
"vite-electron-plugin": "^0.5.0",
23+
"vite-plugin-electron": "^0.10.4",
2324
"vite-plugin-electron-renderer": "^0.11.1",
2425
"vue": "^3.2.40",
2526
"vue-tsc": "^1.0.9"
@@ -36,4 +37,4 @@
3637
"vue3",
3738
"vue"
3839
]
39-
}
40+
}

vite.config.ts

+43-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
11
import { rmSync } from 'fs'
22
import { defineConfig } from 'vite'
33
import vue from '@vitejs/plugin-vue'
4-
import electron from 'vite-electron-plugin'
5-
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
4+
import electron from 'vite-plugin-electron'
65
import renderer from 'vite-plugin-electron-renderer'
76
import pkg from './package.json'
87

98
rmSync('dist-electron', { recursive: true, force: true })
9+
const sourcemap = !!process.env.VSCODE_DEBUG
10+
const isBuild = process.argv.slice(2).includes('build')
1011

1112
// https://vitejs.dev/config/
1213
export default defineConfig({
1314
plugins: [
1415
vue(),
15-
electron({
16-
include: ['electron'],
17-
transformOptions: {
18-
sourcemap: !!process.env.VSCODE_DEBUG,
16+
electron([
17+
{
18+
// Main-Process entry file of the Electron App.
19+
entry: 'electron/main/index.ts',
20+
onstart(options) {
21+
if (process.env.VSCODE_DEBUG) {
22+
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
23+
} else {
24+
options.startup()
25+
}
26+
},
27+
vite: {
28+
build: {
29+
sourcemap,
30+
minify: isBuild,
31+
outDir: 'dist-electron/main',
32+
rollupOptions: {
33+
external: Object.keys(pkg.dependencies),
34+
},
35+
},
36+
},
1937
},
20-
plugins: [
21-
...(process.env.VSCODE_DEBUG
22-
? [
23-
// Will start Electron via VSCode Debug
24-
customStart(debounce(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App'))),
25-
]
26-
: []),
27-
// Allow use `import.meta.env.VITE_SOME_KEY` in Electron-Main
28-
loadViteEnv(),
29-
],
30-
}),
38+
{
39+
entry: 'electron/preload/index.ts',
40+
onstart(options) {
41+
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
42+
// instead of restarting the entire Electron App.
43+
options.reload()
44+
},
45+
vite: {
46+
build: {
47+
sourcemap,
48+
minify: isBuild,
49+
outDir: 'dist-electron/preload',
50+
rollupOptions: {
51+
external: Object.keys(pkg.dependencies),
52+
},
53+
},
54+
},
55+
}
56+
]),
3157
// Use Node.js API in the Renderer-process
3258
renderer({
3359
nodeIntegration: true,
@@ -51,11 +77,3 @@ export default defineConfig({
5177
assetsDir: '', // #287
5278
},
5379
})
54-
55-
function debounce<Fn extends (...args: any[]) => void>(fn: Fn, delay = 299) {
56-
let t: NodeJS.Timeout
57-
return ((...args) => {
58-
clearTimeout(t)
59-
t = setTimeout(() => fn(...args), delay)
60-
}) as Fn
61-
}

0 commit comments

Comments
 (0)