refactor: better Debug config 🐞
This commit is contained in:
		
							parent
							
								
									3d0948c801
								
							
						
					
					
						commit
						05cdb82d15
					
				| 
						 | 
					@ -1,12 +1,8 @@
 | 
				
			||||||
import { rmSync } from 'fs'
 | 
					import { rmSync } from 'fs'
 | 
				
			||||||
import path from 'path'
 | 
					import path from 'path'
 | 
				
			||||||
import {
 | 
					import { defineConfig } from 'vite'
 | 
				
			||||||
  type Plugin,
 | 
					 | 
				
			||||||
  type UserConfig,
 | 
					 | 
				
			||||||
  defineConfig,
 | 
					 | 
				
			||||||
} from 'vite'
 | 
					 | 
				
			||||||
import react from '@vitejs/plugin-react'
 | 
					import react from '@vitejs/plugin-react'
 | 
				
			||||||
import electron from 'vite-plugin-electron'
 | 
					import electron, { onstart } from 'vite-plugin-electron'
 | 
				
			||||||
import pkg from './package.json'
 | 
					import pkg from './package.json'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
rmSync(path.join(__dirname, 'dist'), { recursive: true, force: true }) // v14.14.0
 | 
					rmSync(path.join(__dirname, 'dist'), { recursive: true, force: true }) // v14.14.0
 | 
				
			||||||
| 
						 | 
					@ -24,11 +20,14 @@ export default defineConfig({
 | 
				
			||||||
    electron({
 | 
					    electron({
 | 
				
			||||||
      main: {
 | 
					      main: {
 | 
				
			||||||
        entry: 'electron/main/index.ts',
 | 
					        entry: 'electron/main/index.ts',
 | 
				
			||||||
        vite: withDebug({
 | 
					        vite: {
 | 
				
			||||||
          build: {
 | 
					          build: {
 | 
				
			||||||
 | 
					            // For debug
 | 
				
			||||||
 | 
					            sourcemap: true,
 | 
				
			||||||
            outDir: 'dist/electron/main',
 | 
					            outDir: 'dist/electron/main',
 | 
				
			||||||
          },
 | 
					          },
 | 
				
			||||||
        }),
 | 
					          plugins: [process.env.VSCODE_DEBUG ? onstart() : null],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      preload: {
 | 
					      preload: {
 | 
				
			||||||
        input: {
 | 
					        input: {
 | 
				
			||||||
| 
						 | 
					@ -56,18 +55,3 @@ export default defineConfig({
 | 
				
			||||||
    minify: false
 | 
					    minify: false
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					 | 
				
			||||||
function withDebug(config: UserConfig): UserConfig {
 | 
					 | 
				
			||||||
  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)
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
    })
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  return config
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue