electron-vite-react/vite.config.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-12-22 11:31:38 +08:00
import { rmSync } from 'node:fs'
import path from 'node:path'
2022-08-24 19:34:02 +08:00
import { defineConfig } from 'vite'
2022-06-05 08:14:45 +08:00
import react from '@vitejs/plugin-react'
import electron from 'vite-electron-plugin'
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
2022-10-19 19:11:36 +08:00
import renderer from 'vite-plugin-electron-renderer'
import pkg from './package.json'
2022-06-05 08:33:10 +08:00
2023-01-14 08:47:31 +08:00
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
rmSync('dist-electron', { recursive: true, force: true })
2022-06-05 08:14:45 +08:00
2023-01-14 08:47:31 +08:00
const sourcemap = command === 'serve' || !!process.env.VSCODE_DEBUG
2022-12-22 11:31:38 +08:00
2023-01-14 08:47:31 +08:00
return {
resolve: {
alias: {
'@': path.join(__dirname, 'src')
2022-06-05 08:33:10 +08:00
},
2023-01-14 08:47:31 +08:00
},
plugins: [
react(),
electron({
include: [
'electron'
],
transformOptions: {
sourcemap,
},
plugins: [
...(!!process.env.VSCODE_DEBUG
? [
// Will start Electron via VSCode Debug
2023-02-23 09:05:33 +08:00
customStart(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')),
2023-01-14 08:47:31 +08:00
]
: []),
// Allow use `import.meta.env.VITE_SOME_KEY` in Electron-Main
loadViteEnv(),
],
}),
// Use Node.js API in the Renderer-process
renderer(),
2023-01-14 08:47:31 +08:00
],
server: !!process.env.VSCODE_DEBUG ? (() => {
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
return {
host: url.hostname,
port: +url.port,
}
})() : undefined,
clearScreen: false,
}
2022-06-05 08:14:45 +08:00
})