feat(cli): supports specifying electron entry file (#270)

This commit is contained in:
alex8088 2023-09-08 00:32:43 +08:00
parent 8b839872f8
commit 01163866fb
2 changed files with 18 additions and 1 deletions

View file

@ -24,6 +24,7 @@ interface GlobalCLIOptions {
w?: boolean
watch?: boolean
outDir?: string
entry?: string
}
interface DevCLIOptions {
@ -59,6 +60,7 @@ cli
.option('--ignoreConfigWarning', `[boolean] ignore config warning`)
.option('--sourcemap', `[boolean] output source maps for debug (default: false)`)
.option('--outDir <dir>', `[string] output directory (default: out)`)
.option('--entry <file>', `[string] specify electron entry file`)
// dev
cli
@ -83,6 +85,10 @@ cli
process.env.V8_INSPECTOR_BRK_PORT = typeof options.inspectBrk === 'number' ? `${options.inspectBrk}` : '5858'
}
if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry
}
const { createServer } = await import('./server')
const inlineConfig = createInlineConfig(root, options)
@ -103,6 +109,10 @@ cli.command('build [root]', 'build for production').action(async (root: string,
const { build } = await import('./build')
const inlineConfig = createInlineConfig(root, options)
if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry
}
try {
await build(inlineConfig)
} catch (e) {
@ -120,6 +130,10 @@ cli
const { preview } = await import('./preview')
const inlineConfig = createInlineConfig(root, options)
if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry
}
try {
await preview(inlineConfig, { skipBuild: options.skipBuild })
} catch (e) {

View file

@ -6,6 +6,7 @@ import { type ChildProcess, spawn } from 'node:child_process'
const _require = createRequire(import.meta.url)
const ensureElectronEntryFile = (root = process.cwd()): void => {
if (process.env.ELECTRON_ENTRY) return
const pkg = path.join(root, 'package.json')
if (fs.existsSync(pkg)) {
const main = require(pkg).main
@ -133,7 +134,9 @@ export function startElectron(root: string | undefined): ChildProcess {
args.push(`--inspect-brk=${process.env.V8_INSPECTOR_BRK_PORT}`)
}
const ps = spawn(electronPath, ['.'].concat(args), { stdio: 'inherit' })
const entry = process.env.ELECTRON_ENTRY || '.'
const ps = spawn(electronPath, [entry].concat(args), { stdio: 'inherit' })
ps.on('close', process.exit)
return ps