diff --git a/.vscode/.debug.script.mjs b/.vscode/.debug.script.mjs index a315617..2bd79e3 100644 --- a/.vscode/.debug.script.mjs +++ b/.vscode/.debug.script.mjs @@ -2,6 +2,7 @@ import fs from 'fs' import path from 'path' import { fileURLToPath } from 'url' import { createRequire } from 'module' +import { spawn } from 'child_process' const pkg = createRequire(import.meta.url)('../package.json') 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}`) fs.writeFileSync(path.join(__dirname, '.debug.env'), envContent.join('\n')) +// For Debug +fs.writeFileSync(path.join(__dirname, '../node_modules/.electron-vite-debug'), '') + // bootstrap -import('../scripts/watch.mjs?debug=vscode') +spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', 'dev'], { stdio: 'inherit' }) diff --git a/.vscode/launch.json b/.vscode/launch.json index a53b0fc..52ac762 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "compounds": [ { "name": "Debug App", - "preLaunchTask": "start .debug.script.mjs", + "preLaunchTask": "Before Debug", "configurations": [ "Debug Main Process", "Debug Renderer Process" @@ -30,7 +30,7 @@ }, "runtimeArgs": [ "--remote-debugging-port=9229", - "${workspaceRoot}/dist/main/index.cjs" + "." ], "envFile": "${workspaceFolder}/.vscode/.debug.env" }, diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c915b16..ca00c2b 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ "version": "2.0.0", "tasks": [ { - "label": "start .debug.script.mjs", + "label": "Before Debug", "type": "shell", "command": "node .vscode/.debug.script.mjs", "isBackground": true, diff --git a/vite.config.ts b/vite.config.ts index d86bf24..e61e0f4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,6 +1,6 @@ -import { rmSync } from 'fs' +feat(🌱): support Debug in VSCodeimport { existsSync, rmSync } from 'fs' import { join } from 'path' -import { defineConfig } from 'vite' +import { defineConfig, Plugin, UserConfig } from 'vite' import vue from '@vitejs/plugin-vue' import electron from 'vite-plugin-electron' import pkg from './package.json' @@ -14,11 +14,11 @@ export default defineConfig({ electron({ main: { entry: 'electron/main/index.ts', - vite: { + vite: withDebug({ build: { outDir: 'dist/electron/main', }, - }, + }), }, preload: { input: { @@ -27,7 +27,7 @@ export default defineConfig({ }, vite: { build: { - // For debug + // For Debug sourcemap: 'inline', outDir: 'dist/electron/preload', }, @@ -42,3 +42,22 @@ export default defineConfig({ 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 +}