electron-vite-vue/vite.config.ts

73 lines
2.4 KiB
TypeScript
Raw Normal View History

import fs from 'node:fs'
2022-12-22 01:58:54 +08:00
import { defineConfig } from 'vite'
2022-06-13 22:10:18 +08:00
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron/simple'
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 }) => {
fs.rmSync('dist-electron', { recursive: true, force: true })
2022-12-22 01:58:54 +08: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({
main: {
// Shortcut of `build.lib.entry`
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 : {}),
},
},
},
},
preload: {
// Shortcut of `build.rollupOptions.input`.
// Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
input: 'electron/preload/index.ts',
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 : {}),
},
},
},
},
// 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
})