Merge pull request #200 from electron-vite/dev

support Debug in VSCode  https://github.com/electron-vite/electron-vite-vue/issues/161
This commit is contained in:
草鞋没号 2022-07-23 17:34:11 +08:00 committed by GitHub
commit 7e1fd6e1d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path' import path from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { createRequire } from 'module' import { createRequire } from 'module'
import { spawn } from 'child_process'
const pkg = createRequire(import.meta.url)('../package.json') const pkg = createRequire(import.meta.url)('../package.json')
const __dirname = path.dirname(fileURLToPath(import.meta.url)) const __dirname = path.dirname(fileURLToPath(import.meta.url))
@ -10,5 +11,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
const envContent = Object.entries(pkg.env).map(([key, val]) => `${key}=${val}`) const envContent = Object.entries(pkg.env).map(([key, val]) => `${key}=${val}`)
fs.writeFileSync(path.join(__dirname, '.debug.env'), envContent.join('\n')) fs.writeFileSync(path.join(__dirname, '.debug.env'), envContent.join('\n'))
// For Debug
fs.writeFileSync(path.join(__dirname, '../node_modules/.electron-vite-debug'), '')
// bootstrap // bootstrap
import('../scripts/watch.mjs?debug=vscode') spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', 'dev'], { stdio: 'inherit' })

4
.vscode/launch.json vendored
View File

@ -6,7 +6,7 @@
"compounds": [ "compounds": [
{ {
"name": "Debug App", "name": "Debug App",
"preLaunchTask": "start .debug.script.mjs", "preLaunchTask": "Before Debug",
"configurations": [ "configurations": [
"Debug Main Process", "Debug Main Process",
"Debug Renderer Process" "Debug Renderer Process"
@ -30,7 +30,7 @@
}, },
"runtimeArgs": [ "runtimeArgs": [
"--remote-debugging-port=9229", "--remote-debugging-port=9229",
"${workspaceRoot}/dist/main/index.cjs" "."
], ],
"envFile": "${workspaceFolder}/.vscode/.debug.env" "envFile": "${workspaceFolder}/.vscode/.debug.env"
}, },

2
.vscode/tasks.json vendored
View File

@ -4,7 +4,7 @@
"version": "2.0.0", "version": "2.0.0",
"tasks": [ "tasks": [
{ {
"label": "start .debug.script.mjs", "label": "Before Debug",
"type": "shell", "type": "shell",
"command": "node .vscode/.debug.script.mjs", "command": "node .vscode/.debug.script.mjs",
"isBackground": true, "isBackground": true,

View File

@ -1,6 +1,6 @@
import { rmSync } from 'fs' feat(🌱): support Debug in VSCodeimport { existsSync, rmSync } from 'fs'
import { join } from 'path' import { join } from 'path'
import { defineConfig } from 'vite' import { defineConfig, Plugin, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron' import electron from 'vite-plugin-electron'
import pkg from './package.json' import pkg from './package.json'
@ -14,11 +14,11 @@ export default defineConfig({
electron({ electron({
main: { main: {
entry: 'electron/main/index.ts', entry: 'electron/main/index.ts',
vite: { vite: withDebug({
build: { build: {
outDir: 'dist/electron/main', outDir: 'dist/electron/main',
}, },
}, }),
}, },
preload: { preload: {
input: { input: {
@ -27,7 +27,7 @@ export default defineConfig({
}, },
vite: { vite: {
build: { build: {
// For debug // For Debug
sourcemap: 'inline', sourcemap: 'inline',
outDir: 'dist/electron/preload', outDir: 'dist/electron/preload',
}, },
@ -42,3 +42,22 @@ export default defineConfig({
port: pkg.env.VITE_DEV_SERVER_PORT, port: pkg.env.VITE_DEV_SERVER_PORT,
}, },
}) })
function withDebug(config: UserConfig): UserConfig {
const DebugFile = join(__dirname, 'node_modules/.electron-vite-debug')
const isDebug = existsSync(DebugFile)
if (isDebug) {
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');
(config.plugins as Plugin[]).splice(index, 1)
rmSync(DebugFile)
},
})
}
return config
}