refactor: use watch.mjs in .debug.script.mjs

This commit is contained in:
草鞋没号 2022-03-15 07:56:49 +08:00
parent 1651b5dd2a
commit 9336e41591
2 changed files with 15 additions and 54 deletions

View File

@ -1,47 +1 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { createRequire } from 'module'
import { build, createServer } from 'vite'
const pkg = createRequire(import.meta.url)('../package.json')
const __dirname = path.dirname(fileURLToPath(import.meta.url))
async function buildDebugApp() {
const server = await createServer({ configFile: 'packages/renderer/vite.config.ts' })
await server.listen()
await build({
configFile: 'packages/preload/vite.config.ts',
build: {
watch: true,
sourcemap: true,
},
mode: 'development',
})
await build({
configFile: 'packages/main/vite.config.ts',
build: {
watch: true,
sourcemap: true,
},
mode: 'development',
plugins: [{
name: 'electron-preload-watcher',
writeBundle() {
server.ws.send({ type: 'full-reload' })
},
}],
})
}
function writeEnvLocal() {
const envContent = Object.entries(pkg.env).map(([key, val]) => `${key}=${val}`)
fs.writeFileSync(path.join(__dirname, '.debug.env'), envContent.join('\n'))
}
// bootstrap
writeEnvLocal()
buildDebugApp()
import '../scripts/watch.mjs?debug=vscode'

View File

@ -2,6 +2,9 @@ import { spawn } from 'child_process'
import { createServer, build } from 'vite'
import electron from 'electron'
const query = new URLSearchParams(import.meta.url.split('?')[1])
const debug = query.has('debug')
/**
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
*/
@ -15,17 +18,21 @@ function watchMain(server) {
VITE_DEV_SERVER_HOST: address.address,
VITE_DEV_SERVER_PORT: address.port,
})
/**
* @type {import('vite').Plugin}
*/
const startElectron = {
name: 'electron-main-watcher',
writeBundle() {
electronProcess && electronProcess.kill()
electronProcess = spawn(electron, ['.'], { stdio: 'inherit', env })
},
}
return build({
configFile: 'packages/main/vite.config.ts',
mode: 'development',
plugins: [{
name: 'electron-main-watcher',
writeBundle() {
electronProcess && electronProcess.kill()
electronProcess = spawn(electron, ['.'], { stdio: 'inherit', env })
},
}],
plugins: [!debug && startElectron].filter(Boolean),
build: {
watch: true,
},