8000 refactor: `vite-plugin-electron` instead `vite-electron-plugin` · coder-long/electron-vite-react@e93d649 · GitHub
[go: up one dir, main page]

Skip to content

Commit e93d649

Browse files
committed
refactor: vite-plugin-electron instead vite-electron-plugin
1 parent 1c707b4 commit e93d649

File tree

2 files changed

+46
-107
lines changed

2 files changed

+46
-107
lines changed

vite.config.ts

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { rmSync } from 'node:fs'
22
import path from 'node:path'
33
import { defineConfig } from 'vite'
44
import react from '@vitejs/plugin-react'
5-
import electron from 'vite-electron-plugin'
6-
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
5+
import electron from 'vite-plugin-electron'
76
import renderer from 'vite-plugin-electron-renderer'
87
import pkg from './package.json'
98

109
// https://vitejs.dev/config/
1110
export default defineConfig(({ command }) => {
1211
rmSync('dist-electron', { recursive: true, force: true })
1312

14-
const sourcemap = command === 'serve' || !!process.env.VSCODE_DEBUG
13+
const isServe = command === 'serve'
14+
const isBuild = command === 'build'
15+
const sourcemap = isServe || !!process.env.VSCODE_DEBUG
1516

1617
return {
1718
resolve: {
@@ -21,34 +22,57 @@ export default defineConfig(({ command }) => {
2122
},
2223
plugins: [
2324
react(),
24-
electron({
25-
include: [
26-
'electron'
27-
],
28-
transformOptions: {
29-
sourcemap,
25+
electron([
26+
{
27+
// Main-Process entry file of the Electron App.
28+
entry: 'electron/main/index.ts',
29+
onstart(options) {
30+
if (process.env.VSCODE_DEBUG) {
31+
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
32+
} else {
33+
options.startup()
34+
}
35+
},
36+
vite: {
37+
build: {
38+
sourcemap,
39+
minify: isBuild,
40+
outDir: 'dist-electron/main',
41+
rollupOptions: {
42+
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
43+
},
44+
},
45+
},
3046
},
31-
plugins: [
32-
...(!!process.env.VSCODE_DEBUG
33-
? [
34-
// Will start Electron via VSCode Debug
35-
customStart(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')),
36-
]
37-
: []),
38-
// Allow use `import.meta.env.VITE_SOME_KEY` in Electron-Main
39-
loadViteEnv(),
40-
],
41-
}),
47+
{
48+
entry: 'electron/preload/index.ts',
49+
onstart(options) {
50+
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
51+
// instead of restarting the entire Electron App.
52+
options.reload()
53+
},
54+
vite: {
55+
build: {
56+
sourcemap: sourcemap ? 'inline' : undefined, // #332
57+
minify: isBuild,
58+
outDir: 'dist-electron/preload',
59+
rollupOptions: {
60+
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
61+
},
62+
},
63+
},
64+
}
65+
]),
4266
// Use Node.js API in the Renderer-process
4367
renderer(),
4468
],
45-
server: !!process.env.VSCODE_DEBUG ? (() => {
69+
server: process.env.VSCODE_DEBUG && (() => {
4670
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
4771
return {
4872
host: url.hostname,
4973
port: +url.port,
5074
}
51-
})() : undefined,
75+
})(),
5276
clearScreen: false,
5377
}
5478
})

vite.legacy.config.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0