2023-10-04 17:54:50 +00:00
|
|
|
import { rmSync } from 'node:fs'
|
2023-09-02 11:37:03 +00:00
|
|
|
import { defineConfig } from 'vite'
|
2023-10-04 17:54:50 +00:00
|
|
|
import vue from '@vitejs/plugin-vue'
|
2023-09-02 11:37:03 +00:00
|
|
|
import electron from 'vite-plugin-electron'
|
|
|
|
import renderer from 'vite-plugin-electron-renderer'
|
2023-10-04 17:54:50 +00:00
|
|
|
import { notBundle } from 'vite-plugin-electron/plugin'
|
|
|
|
import pkg from './package.json'
|
2023-09-02 11:37:03 +00:00
|
|
|
|
|
|
|
// https://vitejs.dev/config/
|
2023-10-04 17:54:50 +00:00
|
|
|
export default defineConfig(({ command }) => {
|
|
|
|
rmSync('dist-electron', { recursive: true, force: true })
|
2023-09-05 13:28:58 +00:00
|
|
|
|
2023-10-04 17:54:50 +00:00
|
|
|
const isServe = command === 'serve'
|
|
|
|
const isBuild = command === 'build'
|
|
|
|
const sourcemap = isServe || !!process.env.VSCODE_DEBUG
|
2023-09-05 13:28:58 +00:00
|
|
|
|
2023-10-04 17:54:50 +00:00
|
|
|
return {
|
|
|
|
plugins: [
|
|
|
|
vue(),
|
|
|
|
electron([
|
|
|
|
{
|
|
|
|
// Main process entry file of the Electron App.
|
|
|
|
entry: 'electron/main/index.ts',
|
|
|
|
onstart({ startup }) {
|
|
|
|
if (process.env.VSCODE_DEBUG) {
|
|
|
|
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
|
|
|
|
} else {
|
|
|
|
startup()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
vite: {
|
|
|
|
build: {
|
|
|
|
sourcemap,
|
|
|
|
minify: isBuild,
|
|
|
|
outDir: 'dist-electron/main',
|
|
|
|
rollupOptions: {
|
|
|
|
// 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. :)
|
|
|
|
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-09-05 09:22:00 +00:00
|
|
|
},
|
2023-10-04 17:54:50 +00:00
|
|
|
{
|
|
|
|
entry: 'electron/preload/index.ts',
|
|
|
|
onstart({ reload }) {
|
|
|
|
// Notify the Renderer process to reload the page when the Preload scripts build is complete,
|
|
|
|
// instead of restarting the entire Electron App.
|
|
|
|
reload()
|
|
|
|
},
|
|
|
|
vite: {
|
|
|
|
build: {
|
|
|
|
sourcemap: sourcemap ? 'inline' : undefined, // #332
|
|
|
|
minify: isBuild,
|
|
|
|
outDir: 'dist-electron/preload',
|
|
|
|
rollupOptions: {
|
|
|
|
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
isServe && notBundle(),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
// Use Node.js API in the Renderer process
|
|
|
|
renderer(),
|
2023-09-05 09:22:00 +00:00
|
|
|
],
|
2023-10-04 17:54:50 +00:00
|
|
|
server: process.env.VSCODE_DEBUG && (() => {
|
|
|
|
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
|
|
|
|
return {
|
|
|
|
host: url.hostname,
|
|
|
|
port: +url.port,
|
|
|
|
}
|
|
|
|
})(),
|
|
|
|
clearScreen: false,
|
|
|
|
}
|
2023-09-02 11:37:03 +00:00
|
|
|
})
|