mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-10 22:43:34 +08:00
refactor(config)!: restructure Electron Vite config interfaces
This commit is contained in:
parent
8892bf3679
commit
de70dfe1dc
104
src/config.ts
104
src/config.ts
@ -5,7 +5,6 @@ import { createRequire } from 'node:module'
|
|||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import {
|
import {
|
||||||
type UserConfig as ViteConfig,
|
type UserConfig as ViteConfig,
|
||||||
type UserConfigExport as ViteConfigExport,
|
|
||||||
type ConfigEnv,
|
type ConfigEnv,
|
||||||
type PluginOption,
|
type PluginOption,
|
||||||
type LogLevel,
|
type LogLevel,
|
||||||
@ -33,7 +32,7 @@ import { isObject, isFilePathESM } from './utils'
|
|||||||
|
|
||||||
export { defineConfig as defineViteConfig } from 'vite'
|
export { defineConfig as defineViteConfig } from 'vite'
|
||||||
|
|
||||||
interface ElectronVitePreloadConfig {
|
interface IsolatedEntriesOption {
|
||||||
/**
|
/**
|
||||||
* Build each entry point as an isolated bundle without code splitting.
|
* Build each entry point as an isolated bundle without code splitting.
|
||||||
*
|
*
|
||||||
@ -41,97 +40,71 @@ interface ElectronVitePreloadConfig {
|
|||||||
* preventing automatic code splitting across entries and ensuring each
|
* preventing automatic code splitting across entries and ensuring each
|
||||||
* output file is fully standalone.
|
* output file is fully standalone.
|
||||||
*
|
*
|
||||||
|
* @experimental
|
||||||
* @default false
|
* @default false
|
||||||
*/
|
*/
|
||||||
isolatedEntries?: boolean
|
isolatedEntries?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ElectronViteRendererConfig {
|
export interface MainViteConfig extends ViteConfig {}
|
||||||
/**
|
|
||||||
* Build each entry point as an isolated bundle without code splitting.
|
export interface PreloadViteConfig extends ViteConfig, IsolatedEntriesOption {}
|
||||||
*
|
|
||||||
* When enabled, each entry will include all its dependencies inline,
|
export interface RendererViteConfig extends ViteConfig, IsolatedEntriesOption {}
|
||||||
* preventing automatic code splitting across entries and ensuring each
|
|
||||||
* output file is fully standalone.
|
|
||||||
*
|
|
||||||
* @default false
|
|
||||||
*/
|
|
||||||
isolatedEntries?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UserConfig {
|
export interface UserConfig {
|
||||||
/**
|
/**
|
||||||
* Vite config options for electron main process
|
* Vite config options for electron main process
|
||||||
*
|
*
|
||||||
* https://vitejs.dev/config/
|
* @see https://vitejs.dev/config/
|
||||||
*/
|
*/
|
||||||
main?: ViteConfig & { configFile?: string | false }
|
main?: MainViteConfig
|
||||||
/**
|
/**
|
||||||
* Vite config options for electron renderer process
|
* Vite config options for electron renderer process
|
||||||
*
|
*
|
||||||
* https://vitejs.dev/config/
|
* @see https://vitejs.dev/config/
|
||||||
*/
|
*/
|
||||||
renderer?: ViteConfig & { configFile?: string | false } & ElectronViteRendererConfig
|
renderer?: RendererViteConfig
|
||||||
/**
|
/**
|
||||||
* Vite config options for electron preload files
|
* Vite config options for electron preload scripts
|
||||||
*
|
*
|
||||||
* https://vitejs.dev/config/
|
* @see https://vitejs.dev/config/
|
||||||
*/
|
*/
|
||||||
preload?: ViteConfig & { configFile?: string | false } & ElectronVitePreloadConfig
|
preload?: PreloadViteConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ElectronViteConfig {
|
export type ElectronViteConfigFnObject = (env: ConfigEnv) => UserConfig
|
||||||
/**
|
export type ElectronViteConfigFnPromise = (env: ConfigEnv) => Promise<UserConfig>
|
||||||
* Vite config options for electron main process
|
export type ElectronViteConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>
|
||||||
*
|
|
||||||
* https://vitejs.dev/config/
|
|
||||||
*/
|
|
||||||
main?: ViteConfigExport
|
|
||||||
/**
|
|
||||||
* Vite config options for electron renderer process
|
|
||||||
*
|
|
||||||
* https://vitejs.dev/config/
|
|
||||||
*/
|
|
||||||
renderer?: ViteConfigExport & ElectronViteRendererConfig
|
|
||||||
/**
|
|
||||||
* Vite config options for electron preload files
|
|
||||||
*
|
|
||||||
* https://vitejs.dev/config/
|
|
||||||
*/
|
|
||||||
preload?: ViteConfigExport & ElectronVitePreloadConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
export type InlineConfig = Omit<ViteConfig, 'base'> & {
|
|
||||||
configFile?: string | false
|
|
||||||
envFile?: false
|
|
||||||
ignoreConfigWarning?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ElectronViteConfigFnObject = (env: ConfigEnv) => ElectronViteConfig
|
|
||||||
export type ElectronViteConfigFnPromise = (env: ConfigEnv) => Promise<ElectronViteConfig>
|
|
||||||
export type ElectronViteConfigFn = (env: ConfigEnv) => ElectronViteConfig | Promise<ElectronViteConfig>
|
|
||||||
|
|
||||||
export type ElectronViteConfigExport =
|
export type ElectronViteConfigExport =
|
||||||
| ElectronViteConfig
|
| UserConfig
|
||||||
| Promise<ElectronViteConfig>
|
| Promise<UserConfig>
|
||||||
| ElectronViteConfigFnObject
|
| ElectronViteConfigFnObject
|
||||||
| ElectronViteConfigFnPromise
|
| ElectronViteConfigFnPromise
|
||||||
| ElectronViteConfigFn
|
| ElectronViteConfigFn
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type helper to make it easier to use `electron.vite.config.*`
|
* Type helper to make it easier to use `electron.vite.config.*`
|
||||||
* accepts a direct {@link ElectronViteConfig} object, or a function that returns it.
|
* accepts a direct {@link UserConfig} object, or a function that returns it.
|
||||||
* The function receives a object that exposes two properties:
|
* The function receives a object that exposes two properties:
|
||||||
* `command` (either `'build'` or `'serve'`), and `mode`.
|
* `command` (either `'build'` or `'serve'`), and `mode`.
|
||||||
*/
|
*/
|
||||||
export function defineConfig(config: ElectronViteConfig): ElectronViteConfig
|
export function defineConfig(config: UserConfig): UserConfig
|
||||||
export function defineConfig(config: Promise<ElectronViteConfig>): Promise<ElectronViteConfig>
|
export function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>
|
||||||
export function defineConfig(config: ElectronViteConfigFnObject): ElectronViteConfigFnObject
|
export function defineConfig(config: ElectronViteConfigFnObject): ElectronViteConfigFnObject
|
||||||
|
export function defineConfig(config: ElectronViteConfigFnPromise): ElectronViteConfigFnPromise
|
||||||
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport
|
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport
|
||||||
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport {
|
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport {
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type InlineConfig = Omit<ViteConfig, 'base'> & {
|
||||||
|
configFile?: string | false
|
||||||
|
envFile?: false
|
||||||
|
ignoreConfigWarning?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export interface ResolvedConfig {
|
export interface ResolvedConfig {
|
||||||
config?: UserConfig
|
config?: UserConfig
|
||||||
configFile?: string
|
configFile?: string
|
||||||
@ -157,6 +130,7 @@ export async function resolveConfig(
|
|||||||
mode,
|
mode,
|
||||||
command
|
command
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadResult = await loadConfigFromFile(
|
const loadResult = await loadConfigFromFile(
|
||||||
configEnv,
|
configEnv,
|
||||||
configFile,
|
configFile,
|
||||||
@ -164,15 +138,18 @@ export async function resolveConfig(
|
|||||||
config.logLevel,
|
config.logLevel,
|
||||||
config.ignoreConfigWarning
|
config.ignoreConfigWarning
|
||||||
)
|
)
|
||||||
|
|
||||||
if (loadResult) {
|
if (loadResult) {
|
||||||
const root = config.root
|
const root = config.root
|
||||||
delete config.root
|
delete config.root
|
||||||
delete config.configFile
|
delete config.configFile
|
||||||
|
|
||||||
|
config.configFile = false
|
||||||
|
|
||||||
const outDir = config.build?.outDir
|
const outDir = config.build?.outDir
|
||||||
|
|
||||||
if (loadResult.config.main) {
|
if (loadResult.config.main) {
|
||||||
const mainViteConfig: ViteConfig = mergeConfig(loadResult.config.main, deepClone(config))
|
const mainViteConfig: MainViteConfig = mergeConfig(loadResult.config.main, deepClone(config))
|
||||||
|
|
||||||
mainViteConfig.mode = inlineConfig.mode || mainViteConfig.mode || defaultMode
|
mainViteConfig.mode = inlineConfig.mode || mainViteConfig.mode || defaultMode
|
||||||
|
|
||||||
@ -200,14 +177,10 @@ export async function resolveConfig(
|
|||||||
mainViteConfig.plugins = builtInMainPlugins.concat(mainViteConfig.plugins || [])
|
mainViteConfig.plugins = builtInMainPlugins.concat(mainViteConfig.plugins || [])
|
||||||
|
|
||||||
loadResult.config.main = mainViteConfig
|
loadResult.config.main = mainViteConfig
|
||||||
loadResult.config.main.configFile = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadResult.config.preload) {
|
if (loadResult.config.preload) {
|
||||||
const preloadViteConfig: ViteConfig & ElectronVitePreloadConfig = mergeConfig(
|
const preloadViteConfig: PreloadViteConfig = mergeConfig(loadResult.config.preload, deepClone(config))
|
||||||
loadResult.config.preload,
|
|
||||||
deepClone(config)
|
|
||||||
)
|
|
||||||
|
|
||||||
preloadViteConfig.mode = inlineConfig.mode || preloadViteConfig.mode || defaultMode
|
preloadViteConfig.mode = inlineConfig.mode || preloadViteConfig.mode || defaultMode
|
||||||
|
|
||||||
@ -244,14 +217,10 @@ export async function resolveConfig(
|
|||||||
preloadViteConfig.plugins = builtInPreloadPlugins.concat(preloadViteConfig.plugins)
|
preloadViteConfig.plugins = builtInPreloadPlugins.concat(preloadViteConfig.plugins)
|
||||||
|
|
||||||
loadResult.config.preload = preloadViteConfig
|
loadResult.config.preload = preloadViteConfig
|
||||||
loadResult.config.preload.configFile = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadResult.config.renderer) {
|
if (loadResult.config.renderer) {
|
||||||
const rendererViteConfig: ViteConfig & ElectronViteRendererConfig = mergeConfig(
|
const rendererViteConfig: RendererViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config))
|
||||||
loadResult.config.renderer,
|
|
||||||
deepClone(config)
|
|
||||||
)
|
|
||||||
|
|
||||||
rendererViteConfig.mode = inlineConfig.mode || rendererViteConfig.mode || defaultMode
|
rendererViteConfig.mode = inlineConfig.mode || rendererViteConfig.mode || defaultMode
|
||||||
|
|
||||||
@ -280,7 +249,6 @@ export async function resolveConfig(
|
|||||||
rendererViteConfig.plugins = builtInRendererPlugins.concat(rendererViteConfig.plugins || [])
|
rendererViteConfig.plugins = builtInRendererPlugins.concat(rendererViteConfig.plugins || [])
|
||||||
|
|
||||||
loadResult.config.renderer = rendererViteConfig
|
loadResult.config.renderer = rendererViteConfig
|
||||||
loadResult.config.renderer.configFile = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
userConfig = loadResult.config
|
userConfig = loadResult.config
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user