refactor: better Debug logic
This commit is contained in:
		
							parent
							
								
									58e568506a
								
							
						
					
					
						commit
						f9571ab94a
					
				| 
						 | 
					@ -12,13 +12,13 @@ const pkg = require('../package.json')
 | 
				
			||||||
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
 | 
				
			||||||
spawn(
 | 
					spawn(
 | 
				
			||||||
  // TODO: terminate `npm run dev` when Debug exits.
 | 
					  // TODO: terminate `npm run dev` when Debug exits.
 | 
				
			||||||
  process.platform === 'win32' ? 'npm.cmd' : 'npm',
 | 
					  process.platform === 'win32' ? 'npm.cmd' : 'npm',
 | 
				
			||||||
  ['run', 'dev'],
 | 
					  ['run', 'dev'],
 | 
				
			||||||
  { stdio: 'inherit' },
 | 
					  {
 | 
				
			||||||
 | 
					    stdio: 'inherit',
 | 
				
			||||||
 | 
					    env: Object.assign(process.env, { VSCODE_DEBUG: 'true' }),
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
import { existsSync, rmSync } from 'fs'
 | 
					import { rmSync } from 'fs'
 | 
				
			||||||
import { join } from 'path'
 | 
					import { join } from 'path'
 | 
				
			||||||
import { defineConfig, UserConfig, Plugin } from 'vite'
 | 
					import { defineConfig, UserConfig, Plugin } from 'vite'
 | 
				
			||||||
import react from '@vitejs/plugin-react'
 | 
					import react from '@vitejs/plugin-react'
 | 
				
			||||||
| 
						 | 
					@ -50,10 +50,7 @@ export default defineConfig({
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function withDebug(config: UserConfig): UserConfig {
 | 
					function withDebug(config: UserConfig): UserConfig {
 | 
				
			||||||
  const debugFile = join(__dirname, 'node_modules/.electron-vite-debug')
 | 
					  if (process.env.VSCODE_DEBUG) {
 | 
				
			||||||
  const isDebug = existsSync(debugFile)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (isDebug) {
 | 
					 | 
				
			||||||
    config.build.sourcemap = true
 | 
					    config.build.sourcemap = true
 | 
				
			||||||
    config.plugins = (config.plugins || []).concat({
 | 
					    config.plugins = (config.plugins || []).concat({
 | 
				
			||||||
      name: 'electron-vite-debug',
 | 
					      name: 'electron-vite-debug',
 | 
				
			||||||
| 
						 | 
					@ -61,10 +58,8 @@ function withDebug(config: UserConfig): UserConfig {
 | 
				
			||||||
        // TODO: when the next version of `vite-plugine-electron` is released, use the config hook.
 | 
					        // TODO: when the next version of `vite-plugine-electron` is released, use the config hook.
 | 
				
			||||||
        const index = config.plugins.findIndex(p => p.name === 'electron-main-watcher');
 | 
					        const index = config.plugins.findIndex(p => p.name === 'electron-main-watcher');
 | 
				
			||||||
        (config.plugins as Plugin[]).splice(index, 1)
 | 
					        (config.plugins as Plugin[]).splice(index, 1)
 | 
				
			||||||
        rmSync(debugFile)
 | 
					 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  return config
 | 
					  return config
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue