feat: add --rendererOnly flag to dev command

This commit is contained in:
alex8088 2023-04-23 21:28:58 +08:00
parent a736af835e
commit f443c18056
2 changed files with 18 additions and 6 deletions

View file

@ -60,7 +60,8 @@ cli
.alias('dev') .alias('dev')
.option('-w, --watch', `[boolean] rebuilds when main process or preload script modules have changed on disk`) .option('-w, --watch', `[boolean] rebuilds when main process or preload script modules have changed on disk`)
.option('--remoteDebuggingPort <port>', `[string] port for remote debugging`) .option('--remoteDebuggingPort <port>', `[string] port for remote debugging`)
.action(async (root: string, options: { remoteDebuggingPort?: string } & GlobalCLIOptions) => { .option('--rendererOnly', `[boolean] only dev server for the renderer`)
.action(async (root: string, options: { remoteDebuggingPort?: string; rendererOnly: boolean } & GlobalCLIOptions) => {
if (options.remoteDebuggingPort) { if (options.remoteDebuggingPort) {
process.env.REMOTE_DEBUGGING_PORT = options.remoteDebuggingPort process.env.REMOTE_DEBUGGING_PORT = options.remoteDebuggingPort
} }
@ -69,7 +70,7 @@ cli
const inlineConfig = createInlineConfig(root, options) const inlineConfig = createInlineConfig(root, options)
try { try {
await createServer(inlineConfig) await createServer(inlineConfig, { rendererOnly: options.rendererOnly })
} catch (e) { } catch (e) {
const error = e as Error const error = e as Error
createLogger(options.logLevel).error( createLogger(options.logLevel).error(

View file

@ -12,7 +12,10 @@ import { type InlineConfig, resolveConfig } from './config'
import { resolveHostname } from './utils' import { resolveHostname } from './utils'
import { startElectron } from './electron' import { startElectron } from './electron'
export async function createServer(inlineConfig: InlineConfig = {}): Promise<void> { export async function createServer(
inlineConfig: InlineConfig = {},
options: { rendererOnly?: boolean }
): Promise<void> {
process.env.NODE_ENV_ELECTRON_VITE = 'development' process.env.NODE_ENV_ELECTRON_VITE = 'development'
const config = await resolveConfig(inlineConfig, 'serve', 'development') const config = await resolveConfig(inlineConfig, 'serve', 'development')
if (config.config) { if (config.config) {
@ -22,7 +25,7 @@ export async function createServer(inlineConfig: InlineConfig = {}): Promise<voi
let ps: ChildProcessWithoutNullStreams | undefined let ps: ChildProcessWithoutNullStreams | undefined
const mainViteConfig = config.config?.main const mainViteConfig = config.config?.main
if (mainViteConfig) { if (mainViteConfig && !options.rendererOnly) {
const watchHook = (): void => { const watchHook = (): void => {
logger.info(colors.green(`\nrebuild the electron main process successfully`)) logger.info(colors.green(`\nrebuild the electron main process successfully`))
@ -43,7 +46,7 @@ export async function createServer(inlineConfig: InlineConfig = {}): Promise<voi
} }
const preloadViteConfig = config.config?.preload const preloadViteConfig = config.config?.preload
if (preloadViteConfig) { if (preloadViteConfig && !options.rendererOnly) {
logger.info(colors.gray(`\n-----\n`)) logger.info(colors.gray(`\n-----\n`))
const watchHook = (): void => { const watchHook = (): void => {
@ -61,6 +64,14 @@ export async function createServer(inlineConfig: InlineConfig = {}): Promise<voi
logger.info(colors.green(`\nbuild the electron preload files successfully`)) logger.info(colors.green(`\nbuild the electron preload files successfully`))
} }
if (options.rendererOnly) {
logger.warn(
`\n${colors.yellow(colors.bold('warn'))}:${colors.yellow(
' you have skipped the main process and preload scripts building'
)}`
)
}
const rendererViteConfig = config.config?.renderer const rendererViteConfig = config.config?.renderer
if (rendererViteConfig) { if (rendererViteConfig) {
logger.info(colors.gray(`\n-----\n`)) logger.info(colors.gray(`\n-----\n`))
@ -83,7 +94,7 @@ export async function createServer(inlineConfig: InlineConfig = {}): Promise<voi
const slogger = server.config.logger const slogger = server.config.logger
slogger.info(colors.green(`dev server running for the electron renderer process at:\n`), { slogger.info(colors.green(`dev server running for the electron renderer process at:\n`), {
clear: !slogger.hasWarned clear: !slogger.hasWarned && !options.rendererOnly
}) })
server.printUrls() server.printUrls()