perf: spawn Electron process using parent's stdios (#236)
This commit is contained in:
parent
9d76799072
commit
342a0556cd
|
@ -1,8 +1,7 @@
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { createRequire } from 'node:module'
|
import { createRequire } from 'node:module'
|
||||||
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
|
import { type ChildProcess, spawn } from 'node:child_process'
|
||||||
import { type Logger } from 'vite'
|
|
||||||
|
|
||||||
const _require = createRequire(import.meta.url)
|
const _require = createRequire(import.meta.url)
|
||||||
|
|
||||||
|
@ -111,13 +110,11 @@ export function getElectronChromeTarget(): string {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startElectron(root: string | undefined, logger: Logger): ChildProcessWithoutNullStreams {
|
export function startElectron(root: string | undefined): ChildProcess {
|
||||||
ensureElectronEntryFile(root)
|
ensureElectronEntryFile(root)
|
||||||
|
|
||||||
const electronPath = getElectronPath()
|
const electronPath = getElectronPath()
|
||||||
|
|
||||||
const inspect = !!process.env.VSCODE_INSPECTOR_OPTIONS
|
|
||||||
|
|
||||||
const isDev = process.env.NODE_ENV_ELECTRON_VITE === 'development'
|
const isDev = process.env.NODE_ENV_ELECTRON_VITE === 'development'
|
||||||
|
|
||||||
const args: string[] = []
|
const args: string[] = []
|
||||||
|
@ -134,13 +131,7 @@ export function startElectron(root: string | undefined, logger: Logger): ChildPr
|
||||||
args.push(`--inspect-brk=${process.env.V8_INSPECTOR_BRK_PORT}`)
|
args.push(`--inspect-brk=${process.env.V8_INSPECTOR_BRK_PORT}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ps = spawn(electronPath, ['.'].concat(args))
|
const ps = spawn(electronPath, ['.'].concat(args), { stdio: 'inherit' })
|
||||||
ps.stdout.on('data', chunk => {
|
|
||||||
!inspect && chunk.toString().trim() && logger.info(chunk.toString())
|
|
||||||
})
|
|
||||||
ps.stderr.on('data', chunk => {
|
|
||||||
!inspect && chunk.toString().trim() && logger.error(chunk.toString())
|
|
||||||
})
|
|
||||||
ps.on('close', process.exit)
|
ps.on('close', process.exit)
|
||||||
|
|
||||||
return ps
|
return ps
|
||||||
|
|
|
@ -11,7 +11,7 @@ export async function preview(inlineConfig: InlineConfig = {}, options: { skipBu
|
||||||
|
|
||||||
const logger = createLogger(inlineConfig.logLevel)
|
const logger = createLogger(inlineConfig.logLevel)
|
||||||
|
|
||||||
startElectron(inlineConfig.root, logger)
|
startElectron(inlineConfig.root)
|
||||||
|
|
||||||
logger.info(colors.green(`\nstart electron app...\n`))
|
logger.info(colors.green(`\nstart electron app...\n`))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import type { ChildProcessWithoutNullStreams } from 'node:child_process'
|
import type { ChildProcess } from 'node:child_process'
|
||||||
import {
|
import {
|
||||||
type UserConfig as ViteConfig,
|
type UserConfig as ViteConfig,
|
||||||
type ViteDevServer,
|
type ViteDevServer,
|
||||||
|
@ -22,7 +22,7 @@ export async function createServer(
|
||||||
const logger = createLogger(inlineConfig.logLevel)
|
const logger = createLogger(inlineConfig.logLevel)
|
||||||
|
|
||||||
let server: ViteDevServer | undefined
|
let server: ViteDevServer | undefined
|
||||||
let ps: ChildProcessWithoutNullStreams | undefined
|
let ps: ChildProcess | undefined
|
||||||
|
|
||||||
const mainViteConfig = config.config?.main
|
const mainViteConfig = config.config?.main
|
||||||
if (mainViteConfig && !options.rendererOnly) {
|
if (mainViteConfig && !options.rendererOnly) {
|
||||||
|
@ -34,7 +34,7 @@ export async function createServer(
|
||||||
|
|
||||||
ps.removeAllListeners()
|
ps.removeAllListeners()
|
||||||
ps.kill()
|
ps.kill()
|
||||||
ps = startElectron(inlineConfig.root, logger)
|
ps = startElectron(inlineConfig.root)
|
||||||
|
|
||||||
logger.info(colors.green(`\nrestart electron app...`))
|
logger.info(colors.green(`\nrestart electron app...`))
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ export async function createServer(
|
||||||
server.printUrls()
|
server.printUrls()
|
||||||
}
|
}
|
||||||
|
|
||||||
ps = startElectron(inlineConfig.root, logger)
|
ps = startElectron(inlineConfig.root)
|
||||||
|
|
||||||
logger.info(colors.green(`\nstart electron app...\n`))
|
logger.info(colors.green(`\nstart electron app...\n`))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue