mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-01-12 04:36:57 +08:00
feat: support custom Electron package name
This commit is contained in:
parent
91368b6655
commit
25b10f3c3d
@ -96,6 +96,13 @@ export interface PreloadViteConfig extends BaseViteConfig<PreloadBuildOptions> {
|
||||
export interface RendererViteConfig extends BaseViteConfig<RendererBuildOptions> {}
|
||||
|
||||
export interface UserConfig {
|
||||
/**
|
||||
* Custom Electron package name (e.g., '@overwolf/ow-electron').
|
||||
* Can also be set via ELECTRON_PKG_NAME environment variable.
|
||||
*
|
||||
* @default 'electron'
|
||||
*/
|
||||
electronPackage?: string
|
||||
/**
|
||||
* Vite config options for electron main process
|
||||
*
|
||||
@ -313,6 +320,16 @@ export async function resolveConfig(
|
||||
}
|
||||
}
|
||||
|
||||
// Set ELECTRON_PKG_NAME from config or environment variable
|
||||
// Priority: existing env var > config file > default to 'electron'
|
||||
if (!process.env.ELECTRON_PKG_NAME) {
|
||||
if (userConfig?.electronPackage) {
|
||||
process.env.ELECTRON_PKG_NAME = userConfig.electronPackage
|
||||
} else {
|
||||
process.env.ELECTRON_PKG_NAME = 'electron'
|
||||
}
|
||||
}
|
||||
|
||||
const resolved: ResolvedConfig = {
|
||||
config: userConfig,
|
||||
configFile: configFile ? normalizePath(configFile) : undefined,
|
||||
|
||||
@ -6,6 +6,10 @@ import { loadPackageData } from './utils'
|
||||
|
||||
const _require = createRequire(import.meta.url)
|
||||
|
||||
const getElectronPackageName = (): string => {
|
||||
return process.env.ELECTRON_PKG_NAME || 'electron'
|
||||
}
|
||||
|
||||
const ensureElectronEntryFile = (root = process.cwd()): void => {
|
||||
if (process.env.ELECTRON_ENTRY) return
|
||||
const pkg = loadPackageData()
|
||||
@ -26,7 +30,8 @@ const ensureElectronEntryFile = (root = process.cwd()): void => {
|
||||
const getElectronMajorVer = (): string => {
|
||||
let majorVer = process.env.ELECTRON_MAJOR_VER || ''
|
||||
if (!majorVer) {
|
||||
const pkg = _require.resolve('electron/package.json')
|
||||
const electronPkgName = getElectronPackageName()
|
||||
const pkg = _require.resolve(`${electronPkgName}/package.json`)
|
||||
if (fs.existsSync(pkg)) {
|
||||
const version = _require(pkg).version
|
||||
majorVer = version.split('.')[0]
|
||||
@ -49,7 +54,8 @@ export function supportImportMetaPaths(): boolean {
|
||||
export function getElectronPath(): string {
|
||||
let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
|
||||
if (!electronExecPath) {
|
||||
const electronModulePath = path.dirname(_require.resolve('electron'))
|
||||
const electronPkgName = getElectronPackageName()
|
||||
const electronModulePath = path.dirname(_require.resolve(electronPkgName))
|
||||
const pathFile = path.join(electronModulePath, 'path.txt')
|
||||
let executablePath
|
||||
if (fs.existsSync(pathFile)) {
|
||||
@ -59,7 +65,7 @@ export function getElectronPath(): string {
|
||||
electronExecPath = path.join(electronModulePath, 'dist', executablePath)
|
||||
process.env.ELECTRON_EXEC_PATH = electronExecPath
|
||||
} else {
|
||||
throw new Error('Electron uninstall')
|
||||
throw new Error(`Electron package "${electronPkgName}" not found or uninstalled`)
|
||||
}
|
||||
}
|
||||
return electronExecPath
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user