electron-vite-vue/vite.config.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-12-22 00:58:54 +07:00
import { rmSync } from 'node:fs'
import { defineConfig } from 'vite'
2022-06-13 22:10:18 +08:00
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer'
2023-09-08 12:17:24 +08:00
import { notBundle } from 'vite-plugin-electron/plugin'
import pkg from './package.json'
2022-06-13 22:10:18 +08:00
2023-01-11 23:45:22 +08:00
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
rmSync('dist-electron', { recursive: true, force: true })
2022-12-22 00:58:54 +07:00
2023-01-11 23:45:22 +08:00
const isServe = command === 'serve'
const isBuild = command === 'build'
const sourcemap = isServe || !!process.env.VSCODE_DEBUG
2022-06-15 21:47:33 +08:00
2023-01-11 23:45:22 +08:00
return {
plugins: [
vue(),
electron([
{
2023-09-08 12:17:24 +08:00
// Main process entry file of the Electron App.
2023-01-11 23:45:22 +08:00
entry: 'electron/main/index.ts',
2023-09-08 12:17:24 +08:00
onstart({ startup }) {
2023-01-11 23:45:22 +08:00
if (process.env.VSCODE_DEBUG) {
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
} else {
2023-09-08 12:17:24 +08:00
startup()
2023-01-11 23:45:22 +08:00
}
},
vite: {
build: {
sourcemap,
minify: isBuild,
outDir: 'dist-electron/main',
rollupOptions: {
2023-09-08 12:17:24 +08:00
// Some third-party Node.js libraries may not be built correctly by Vite, especially `C/C++` addons,
// we can use `external` to exclude them to ensure they work correctly.
// Others need to put them in `dependencies` to ensure they are collected into `app.asar` after the app is built.
// Of course, this is not absolute, just this way is relatively simple. :)
2023-01-11 23:45:22 +08:00
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
2023-09-08 12:17:24 +08:00
plugins: [
// This is just an option to improve build performance, it's non-deterministic!
// e.g. `import log from 'electron-log'` -> `const log = require('electron-log')`
isServe && notBundle(),
],
},
},
2023-01-11 23:45:22 +08:00
{
entry: 'electron/preload/index.ts',
2023-09-08 12:17:24 +08:00
onstart({ reload }) {
// Notify the Renderer process to reload the page when the Preload scripts build is complete,
2023-01-11 23:45:22 +08:00
// instead of restarting the entire Electron App.
2023-09-08 12:17:24 +08:00
reload()
2023-01-11 23:45:22 +08:00
},
vite: {
build: {
2023-02-02 22:07:51 +08:00
sourcemap: sourcemap ? 'inline' : undefined, // #332
2023-01-11 23:45:22 +08:00
minify: isBuild,
outDir: 'dist-electron/preload',
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
2023-09-08 12:17:24 +08:00
plugins: [
isServe && notBundle(),
],
},
2023-01-11 23:45:22 +08:00
}
]),
2023-09-08 12:17:24 +08:00
// Use Node.js API in the Renderer process
renderer(),
2023-01-11 23:45:22 +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,
}
2023-01-11 23:45:22 +08:00
})(),
clearScreen: false,
}
2022-06-13 22:10:18 +08:00
})