mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-06-04 12:18:16 +08:00
Compare commits
4 Commits
086aececae
...
45deddaa16
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45deddaa16 | ||
|
|
31965d2972 | ||
|
|
4bede21c81 | ||
|
|
25b10f3c3d |
@ -96,6 +96,13 @@ export interface PreloadViteConfig extends BaseViteConfig<PreloadBuildOptions> {
|
|||||||
export interface RendererViteConfig extends BaseViteConfig<RendererBuildOptions> {}
|
export interface RendererViteConfig extends BaseViteConfig<RendererBuildOptions> {}
|
||||||
|
|
||||||
export interface UserConfig {
|
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
|
* Vite config options for electron main process
|
||||||
*
|
*
|
||||||
@ -195,20 +202,14 @@ export async function resolveConfig(
|
|||||||
const { main, preload, renderer } = loadResult.config
|
const { main, preload, renderer } = loadResult.config
|
||||||
|
|
||||||
if (main) {
|
if (main) {
|
||||||
main.build ??= {}
|
|
||||||
setupRollupOptionCompat(main.build)
|
|
||||||
userConfig.main = await new MainConfigFactory(main, config, { outDir, root }).build()
|
userConfig.main = await new MainConfigFactory(main, config, { outDir, root }).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preload) {
|
if (preload) {
|
||||||
preload.build ??= {}
|
|
||||||
setupRollupOptionCompat(preload.build)
|
|
||||||
userConfig.preload = await new PreloadConfigFactory(preload, config, { outDir, root }).build()
|
userConfig.preload = await new PreloadConfigFactory(preload, config, { outDir, root }).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (renderer) {
|
if (renderer) {
|
||||||
renderer.build ??= {}
|
|
||||||
setupRollupOptionCompat(renderer.build)
|
|
||||||
userConfig.renderer = await new RendererConfigFactory(renderer, config, { outDir, root }).build()
|
userConfig.renderer = await new RendererConfigFactory(renderer, config, { outDir, root }).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,6 +218,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 = {
|
const resolved: ResolvedConfig = {
|
||||||
config: userConfig,
|
config: userConfig,
|
||||||
configFile: configFile ? normalizePath(configFile) : undefined,
|
configFile: configFile ? normalizePath(configFile) : undefined,
|
||||||
@ -226,20 +237,15 @@ export async function resolveConfig(
|
|||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupRollupOptionCompat<T extends Pick<ViteBuildOptions, 'rollupOptions' | 'rolldownOptions'>>(
|
|
||||||
buildConfig: T
|
|
||||||
): asserts buildConfig is T & {
|
|
||||||
rolldownOptions: Exclude<T['rolldownOptions'], undefined>
|
|
||||||
} {
|
|
||||||
buildConfig.rolldownOptions ??= buildConfig.rollupOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
export abstract class ConfigFactory<T extends MainViteConfig | PreloadViteConfig | RendererViteConfig> {
|
export abstract class ConfigFactory<T extends MainViteConfig | PreloadViteConfig | RendererViteConfig> {
|
||||||
constructor(
|
constructor(
|
||||||
protected readonly baseConfig: T,
|
protected readonly baseConfig: T,
|
||||||
protected readonly inlineConfig: InlineConfig,
|
protected readonly inlineConfig: InlineConfig,
|
||||||
protected readonly options: { outDir?: string; root?: string }
|
protected readonly options: { outDir?: string; root?: string }
|
||||||
) {}
|
) {
|
||||||
|
baseConfig.build ??= {}
|
||||||
|
baseConfig.build.rolldownOptions ??= baseConfig.build.rollupOptions
|
||||||
|
}
|
||||||
|
|
||||||
async build(cleanMode?: boolean): Promise<T> {
|
async build(cleanMode?: boolean): Promise<T> {
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||||
|
|||||||
@ -6,6 +6,10 @@ import { loadPackageData } from './utils'
|
|||||||
|
|
||||||
const _require = createRequire(import.meta.url)
|
const _require = createRequire(import.meta.url)
|
||||||
|
|
||||||
|
const getElectronPackageName = (): string => {
|
||||||
|
return process.env.ELECTRON_PKG_NAME || 'electron'
|
||||||
|
}
|
||||||
|
|
||||||
const ensureElectronEntryFile = (root = process.cwd()): void => {
|
const ensureElectronEntryFile = (root = process.cwd()): void => {
|
||||||
if (process.env.ELECTRON_ENTRY) return
|
if (process.env.ELECTRON_ENTRY) return
|
||||||
const pkg = loadPackageData()
|
const pkg = loadPackageData()
|
||||||
@ -26,7 +30,8 @@ const ensureElectronEntryFile = (root = process.cwd()): void => {
|
|||||||
const getElectronMajorVer = (): string => {
|
const getElectronMajorVer = (): string => {
|
||||||
let majorVer = process.env.ELECTRON_MAJOR_VER || ''
|
let majorVer = process.env.ELECTRON_MAJOR_VER || ''
|
||||||
if (!majorVer) {
|
if (!majorVer) {
|
||||||
const pkg = _require.resolve('electron/package.json')
|
const electronPkgName = getElectronPackageName()
|
||||||
|
const pkg = _require.resolve(`${electronPkgName}/package.json`)
|
||||||
if (fs.existsSync(pkg)) {
|
if (fs.existsSync(pkg)) {
|
||||||
const version = _require(pkg).version
|
const version = _require(pkg).version
|
||||||
majorVer = version.split('.')[0]
|
majorVer = version.split('.')[0]
|
||||||
@ -49,7 +54,8 @@ export function supportImportMetaPaths(): boolean {
|
|||||||
export function getElectronPath(): string {
|
export function getElectronPath(): string {
|
||||||
let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
|
let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
|
||||||
if (!electronExecPath) {
|
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')
|
const pathFile = path.join(electronModulePath, 'path.txt')
|
||||||
let executablePath
|
let executablePath
|
||||||
if (fs.existsSync(pathFile)) {
|
if (fs.existsSync(pathFile)) {
|
||||||
@ -59,7 +65,7 @@ export function getElectronPath(): string {
|
|||||||
electronExecPath = path.join(electronModulePath, 'dist', executablePath)
|
electronExecPath = path.join(electronModulePath, 'dist', executablePath)
|
||||||
process.env.ELECTRON_EXEC_PATH = electronExecPath
|
process.env.ELECTRON_EXEC_PATH = electronExecPath
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Electron uninstall')
|
throw new Error(`Electron package "${electronPkgName}" not found or uninstalled`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return electronExecPath
|
return electronExecPath
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import path from 'node:path'
|
|||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { builtinModules } from 'node:module'
|
import { builtinModules } from 'node:module'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { type Plugin, type LibraryOptions, type Rolldown, mergeConfig, normalizePath } from 'vite'
|
import { type Plugin, type LibraryOptions, type Rolldown, type UserConfig, mergeConfig, normalizePath } from 'vite'
|
||||||
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
||||||
import { loadPackageData } from '../utils'
|
import { loadPackageData } from '../utils'
|
||||||
|
|
||||||
@ -64,11 +64,6 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
|
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
|
||||||
|
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
resolve: {
|
|
||||||
browserField: false,
|
|
||||||
mainFields: ['module', 'jsnext:main', 'jsnext'],
|
|
||||||
conditions: ['node']
|
|
||||||
},
|
|
||||||
build: {
|
build: {
|
||||||
outDir: path.resolve(root, 'out', 'main'),
|
outDir: path.resolve(root, 'out', 'main'),
|
||||||
target: nodeTarget,
|
target: nodeTarget,
|
||||||
@ -80,7 +75,7 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
reportCompressedSize: false,
|
reportCompressedSize: false,
|
||||||
minify: false
|
minify: false
|
||||||
}
|
}
|
||||||
}
|
} satisfies UserConfig
|
||||||
|
|
||||||
const build = config.build || {}
|
const build = config.build || {}
|
||||||
const rolldownOptions = build.rolldownOptions || {}
|
const rolldownOptions = build.rolldownOptions || {}
|
||||||
@ -106,8 +101,6 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
const buildConfig = mergeConfig(defaultConfig.build, build)
|
const buildConfig = mergeConfig(defaultConfig.build, build)
|
||||||
config.build = buildConfig
|
config.build = buildConfig
|
||||||
|
|
||||||
config.resolve = mergeConfig(defaultConfig.resolve, config.resolve || {})
|
|
||||||
|
|
||||||
config.define = config.define || {}
|
config.define = config.define || {}
|
||||||
config.define = { ...processEnvDefine(), ...config.define }
|
config.define = { ...processEnvDefine(), ...config.define }
|
||||||
|
|
||||||
@ -213,7 +206,7 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
reportCompressedSize: false,
|
reportCompressedSize: false,
|
||||||
minify: false
|
minify: false
|
||||||
}
|
}
|
||||||
}
|
} satisfies UserConfig
|
||||||
|
|
||||||
const build = config.build || {}
|
const build = config.build || {}
|
||||||
const rolldownOptions = build.rolldownOptions || {}
|
const rolldownOptions = build.rolldownOptions || {}
|
||||||
@ -372,7 +365,7 @@ export function electronRendererConfigPresetPlugin(options?: ElectronPluginOptio
|
|||||||
minify: false,
|
minify: false,
|
||||||
emptyOutDir: emptyOutDir()
|
emptyOutDir: emptyOutDir()
|
||||||
}
|
}
|
||||||
}
|
} satisfies UserConfig
|
||||||
|
|
||||||
if (config.build?.outDir) {
|
if (config.build?.outDir) {
|
||||||
config.build.outDir = path.resolve(root, config.build.outDir)
|
config.build.outDir = path.resolve(root, config.build.outDir)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user