electron-vite-react/vite.config.ts

79 lines
2.3 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-plugin-electron'
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
const isServe = command === 'serve'
const isBuild = command === 'build'
const sourcemap = isServe || !!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([
{
// Main-Process entry file of the Electron App.
entry: 'electron/main/index.ts',
onstart(options) {
if (process.env.VSCODE_DEBUG) {
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
} else {
options.startup()
}
},
vite: {
build: {
sourcemap,
minify: isBuild,
outDir: 'dist-electron/main',
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
},
2023-01-14 08:47:31 +08:00
},
{
entry: 'electron/preload/index.ts',
onstart(options) {
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
// instead of restarting the entire Electron App.
options.reload()
},
vite: {
build: {
sourcemap: sourcemap ? 'inline' : undefined, // #332
minify: isBuild,
outDir: 'dist-electron/preload',
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
},
}
]),
2023-01-14 08:47:31 +08:00
// Use Node.js API in the Renderer-process
renderer(),
2023-01-14 08:47:31 +08:00
],
server: process.env.VSCODE_DEBUG && (() => {
2023-01-14 08:47:31 +08:00
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
return {
host: url.hostname,
port: +url.port,
}
})(),
2023-01-14 08:47:31 +08:00
clearScreen: false,
}
2022-06-05 08:14:45 +08:00
})