Skip to content

Commit 281aad2

Browse files
committed
feat: legacy config #115
1 parent 53b1425 commit 281aad2

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

vite.legacy.config.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { rmSync } from 'node:fs'
2+
import path from 'node:path'
3+
import { defineConfig } from 'vite'
4+
import react from '@vitejs/plugin-react'
5+
import electron from 'vite-electron-plugin'
6+
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
7+
import preload from 'vite-plugin-electron'
8+
import renderer from 'vite-plugin-electron-renderer'
9+
import pkg from './package.json'
10+
11+
let preloadHasReady = false
12+
13+
// https://vitejs.dev/config/
14+
export default defineConfig(({ command }) => {
15+
rmSync('dist-electron', { recursive: true, force: true })
16+
17+
const sourcemap = command === 'serve' || !!process.env.VSCODE_DEBUG
18+
19+
return {
20+
resolve: {
21+
alias: {
22+
'@': path.join(__dirname, 'src')
23+
},
24+
},
25+
plugins: [
26+
react(),
27+
electron({
28+
include: [
29+
'electron/main'
30+
],
31+
transformOptions: {
32+
sourcemap,
33+
},
34+
plugins: [
35+
customStart(args => {
36+
if (process.env.VSCODE_DEBUG) {
37+
// Start Electron via VSCode
38+
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
39+
} else {
40+
if (preloadHasReady) {
41+
args?.startup()
42+
} else {
43+
console.log('[startup] waiting for preload')
44+
}
45+
}
46+
}),
47+
// Allow use `import.meta.env.VITE_SOME_KEY` in Main process
48+
loadViteEnv(),
49+
],
50+
}),
51+
// Preload scripts
52+
preload({
53+
entry: [
54+
'electron/preload/index.ts'
55+
],
56+
vite: {
57+
build: {
58+
minify: false,
59+
outDir: 'dist-electron/preload',
60+
},
61+
},
62+
onstart(args) {
63+
if (preloadHasReady) {
64+
args.reload()
65+
} else {
66+
preloadHasReady = true
67+
args.startup()
68+
}
69+
},
70+
}),
71+
// Use Node.js API in the Renderer process
72+
renderer({
73+
nodeIntegration: true,
74+
}),
75+
],
76+
server: !!process.env.VSCODE_DEBUG ? (() => {
77+
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
78+
return {
79+
host: url.hostname,
80+
port: +url.port,
81+
}
82+
})() : undefined,
83+
clearScreen: false,
84+
}
85+
})

0 commit comments

Comments
 (0)