diff --git a/.vscode/.debug.script.mjs b/.vscode/.debug.script.mjs index 2bd79e3..1d68c8f 100644 --- a/.vscode/.debug.script.mjs +++ b/.vscode/.debug.script.mjs @@ -11,8 +11,13 @@ 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 -spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', 'dev'], { stdio: 'inherit' }) +spawn( + // TODO: terminate `npm run dev` when Debug exits. + process.platform === 'win32' ? 'npm.cmd' : 'npm', + ['run', 'dev'], + { + stdio: 'inherit', + env: Object.assign(process.env, { VSCODE_DEBUG: 'true' }), + }, +) diff --git a/vite.config.ts b/vite.config.ts index 6c5bfc6..a1ea3c3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { existsSync, rmSync } from 'fs' +import { rmSync } from 'fs' import { join } from 'path' import { defineConfig, Plugin, UserConfig } from 'vite' import vue from '@vitejs/plugin-vue' @@ -44,20 +44,16 @@ export default defineConfig({ }) function withDebug(config: UserConfig): UserConfig { - const DebugFile = join(__dirname, 'node_modules/.electron-vite-debug') - const isDebug = existsSync(DebugFile) - - if (isDebug) { + if (process.env.VSCODE_DEBUG) { 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) - rmSync(DebugFile) }, }) } - return config }