2022-07-24 07:26:24 +08:00
|
|
|
import { rmSync } from 'fs'
|
2022-12-08 09:24:46 +08:00
|
|
|
import {
|
|
|
|
type Plugin,
|
|
|
|
defineConfig,
|
|
|
|
loadEnv,
|
|
|
|
} from 'vite'
|
2022-06-13 22:10:18 +08:00
|
|
|
import vue from '@vitejs/plugin-vue'
|
2022-11-18 09:20:52 +08:00
|
|
|
import electron from 'vite-plugin-electron'
|
2022-10-18 16:06:39 +08:00
|
|
|
import renderer from 'vite-plugin-electron-renderer'
|
2022-06-30 22:16:12 +08:00
|
|
|
import pkg from './package.json'
|
2022-06-13 22:10:18 +08:00
|
|
|
|
2022-10-03 09:16:14 +08:00
|
|
|
rmSync('dist-electron', { recursive: true, force: true })
|
2022-11-18 09:20:52 +08:00
|
|
|
const sourcemap = !!process.env.VSCODE_DEBUG
|
|
|
|
const isBuild = process.argv.slice(2).includes('build')
|
2022-06-15 21:47:33 +08:00
|
|
|
|
2022-12-08 09:24:46 +08:00
|
|
|
// Load .env
|
|
|
|
function loadEnvPlugin(): Plugin {
|
|
|
|
return {
|
|
|
|
name: 'vite-plugin-load-env',
|
|
|
|
config(config, env) {
|
|
|
|
const root = config.root ?? process.cwd()
|
|
|
|
const result = loadEnv(env.mode, root)
|
|
|
|
// Remove the vite-plugin-electron injected env.
|
|
|
|
delete result.VITE_DEV_SERVER_URL
|
|
|
|
config.esbuild ??= {}
|
|
|
|
config.esbuild.define = {
|
|
|
|
...config.esbuild.define,
|
|
|
|
...Object.fromEntries(Object.entries(result)
|
|
|
|
.map(([key, val]) => [`process.env.${key}`, JSON.stringify(val)])),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 22:10:18 +08:00
|
|
|
// https://vitejs.dev/config/
|
|
|
|
export default defineConfig({
|
2022-06-14 20:53:01 +08:00
|
|
|
plugins: [
|
|
|
|
vue(),
|
2022-11-18 09:20:52 +08:00
|
|
|
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(pkg.dependencies),
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 09:24:46 +08:00
|
|
|
plugins: [loadEnvPlugin()],
|
2022-11-18 09:20:52 +08:00
|
|
|
},
|
2022-06-27 10:26:32 +08:00
|
|
|
},
|
2022-11-18 09:20:52 +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,
|
|
|
|
minify: isBuild,
|
|
|
|
outDir: 'dist-electron/preload',
|
|
|
|
rollupOptions: {
|
|
|
|
external: Object.keys(pkg.dependencies),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]),
|
2022-10-18 16:06:39 +08:00
|
|
|
// Use Node.js API in the Renderer-process
|
|
|
|
renderer({
|
|
|
|
nodeIntegration: true,
|
|
|
|
}),
|
2022-06-14 20:53:01 +08:00
|
|
|
],
|
2022-10-03 09:16:14 +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-13 22:10:18 +08:00
|
|
|
})
|