Merge pull request #248 from electron-vite/dev

refactor: better Debug config
This commit is contained in:
草鞋没号 2022-08-25 06:14:38 +08:00 committed by GitHub
commit 5b21e8f189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 25 deletions

View File

@ -19,7 +19,7 @@
"electron-builder": "^23.3.3", "electron-builder": "^23.3.3",
"typescript": "^4.7.4", "typescript": "^4.7.4",
"vite": "^3.0.8", "vite": "^3.0.8",
"vite-plugin-electron": "^0.9.0", "vite-plugin-electron": "^0.9.1",
"vue": "^3.2.37", "vue": "^3.2.37",
"vue-tsc": "^0.40.1" "vue-tsc": "^0.40.1"
}, },

View File

@ -1,12 +1,8 @@
import { rmSync } from 'fs' import { rmSync } from 'fs'
import path from 'path' import path from 'path'
import { import { defineConfig } from 'vite'
type Plugin,
type UserConfig,
defineConfig,
} from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron' import electron, { onstart } from 'vite-plugin-electron'
import pkg from './package.json' import pkg from './package.json'
rmSync('dist', { recursive: true, force: true }) // v14.14.0 rmSync('dist', { recursive: true, force: true }) // v14.14.0
@ -18,11 +14,15 @@ export default defineConfig({
electron({ electron({
main: { main: {
entry: 'electron/main/index.ts', entry: 'electron/main/index.ts',
vite: withDebug({ vite: {
build: { build: {
// For Debug
sourcemap: true,
outDir: 'dist/electron/main', outDir: 'dist/electron/main',
}, },
}), // Will start Electron via VSCode Debug
plugins: [process.env.VSCODE_DEBUG ? onstart() : null],
},
}, },
preload: { preload: {
input: { input: {
@ -47,19 +47,3 @@ export default defineConfig({
port: pkg.env.VITE_DEV_SERVER_PORT, port: pkg.env.VITE_DEV_SERVER_PORT,
}, },
}) })
function withDebug(config: UserConfig): UserConfig {
if (process.env.VSCODE_DEBUG) {
if (!config.build) config.build = {}
config.build.sourcemap = true
config.plugins = (config.plugins || []).concat({
name: 'electron-vite-debug',
configResolved(config) {
const index = config.plugins.findIndex(p => p.name === 'electron-main-watcher');
// At present, Vite can only modify plugins in configResolved hook.
(config.plugins as Plugin[]).splice(index, 1)
},
})
}
return config
}