Merge pull request #203 from electron-vite/refactor/220724-debug

refactor: better Debug logic
This commit is contained in:
草鞋没号 2022-07-24 07:29:25 +08:00 committed by GitHub
commit 8b740153f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View File

@ -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' }),
},
)

View File

@ -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
}