refactor!: vite-plugin-electron instead vite-electron-plugin

This commit is contained in:
草鞋没号 2022-11-18 09:20:52 +08:00
parent 1fd0096b81
commit b361d2af2c
2 changed files with 46 additions and 27 deletions

View File

@ -13,13 +13,14 @@
"engines": {
"node": "^14.18.0 || >=16.0.0"
},
"dependencies": {},
"devDependencies": {
"@vitejs/plugin-vue": "^3.1.2",
"electron": "^21.1.0",
"electron-builder": "^23.3.3",
"typescript": "^4.8.4",
"vite": "^3.2.2",
"vite-electron-plugin": "^0.5.0",
"vite-plugin-electron": "^0.10.4",
"vite-plugin-electron-renderer": "^0.11.1",
"vue": "^3.2.40",
"vue-tsc": "^1.0.9"

View File

@ -1,33 +1,59 @@
import { rmSync } from 'fs'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from 'vite-electron-plugin'
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer'
import pkg from './package.json'
rmSync('dist-electron', { recursive: true, force: true })
const sourcemap = !!process.env.VSCODE_DEBUG
const isBuild = process.argv.slice(2).includes('build')
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
electron({
include: ['electron'],
transformOptions: {
sourcemap: !!process.env.VSCODE_DEBUG,
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()
}
},
plugins: [
...(process.env.VSCODE_DEBUG
? [
// Will start Electron via VSCode Debug
customStart(debounce(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App'))),
]
: []),
// Allow use `import.meta.env.VITE_SOME_KEY` in Electron-Main
loadViteEnv(),
],
}),
vite: {
build: {
sourcemap,
minify: isBuild,
outDir: 'dist-electron/main',
rollupOptions: {
external: Object.keys(pkg.dependencies),
},
},
},
},
{
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),
},
},
},
}
]),
// Use Node.js API in the Renderer-process
renderer({
nodeIntegration: true,
@ -51,11 +77,3 @@ export default defineConfig({
assetsDir: '', // #287
},
})
function debounce<Fn extends (...args: any[]) => void>(fn: Fn, delay = 299) {
let t: NodeJS.Timeout
return ((...args) => {
clearTimeout(t)
t = setTimeout(() => fn(...args), delay)
}) as Fn
}