77 type UserConfig as ViteConfig ,
88 type ConfigEnv ,
99 type PluginOption ,
10+ type Plugin ,
11+ type BuildEnvironmentOptions as ViteBuildOptions ,
1012 type LogLevel ,
1113 createLogger ,
1214 mergeConfig ,
@@ -28,7 +30,9 @@ import importMetaPlugin from './plugins/importMeta'
2830import esmShimPlugin from './plugins/esmShim'
2931import modulePathPlugin from './plugins/modulePath'
3032import isolateEntriesPlugin from './plugins/isolateEntries'
31- import { isObject , isFilePathESM , deepClone } from './utils'
33+ import { type ExternalOptions , externalizeDepsPlugin } from './plugins/externalizeDeps'
34+ import { type BytecodeOptions , bytecodePlugin } from './plugins/bytecode'
35+ import { isObject , isFilePathESM , deepClone , asyncFlatten } from './utils'
3236
3337export { defineConfig as defineViteConfig } from 'vite'
3438
@@ -46,11 +50,45 @@ interface IsolatedEntriesOption {
4650 isolatedEntries ?: boolean
4751}
4852
49- export interface MainViteConfig extends ViteConfig { }
53+ interface ExternalizeDepsMixin {
54+ /**
55+ * Options pass on to `externalizeDeps` plugin in electron-vite.
56+ *
57+ * Automatically externalize dependencies.
58+ *
59+ * @default true
60+ */
61+ externalizeDeps ?: boolean | ExternalOptions
62+ }
63+
64+ interface BytecodeMixin {
65+ /**
66+ * Options pass on to `bytecode` plugin in electron-vite.
67+ * https://electron-vite.org/guide/source-code-protection#bytecodeplugin-options
68+ *
69+ * Compile source code to v8 bytecode.
70+ */
71+ bytecode ?: boolean | BytecodeOptions
72+ }
73+
74+ interface MainBuildOptions extends ViteBuildOptions , ExternalizeDepsMixin , BytecodeMixin { }
75+
76+ interface PreloadBuildOptions extends ViteBuildOptions , ExternalizeDepsMixin , BytecodeMixin { }
77+
78+ interface RendererBuildOptions extends ViteBuildOptions { }
79+
80+ interface BaseViteConfig < T > extends Omit < ViteConfig , 'build' > {
81+ /**
82+ * Build specific options
83+ */
84+ build ?: T
85+ }
86+
87+ export interface MainViteConfig extends BaseViteConfig < MainBuildOptions > { }
5088
51- export interface PreloadViteConfig extends ViteConfig , IsolatedEntriesOption { }
89+ export interface PreloadViteConfig extends BaseViteConfig < PreloadBuildOptions > , IsolatedEntriesOption { }
5290
53- export interface RendererViteConfig extends ViteConfig , IsolatedEntriesOption { }
91+ export interface RendererViteConfig extends BaseViteConfig < RendererBuildOptions > , IsolatedEntriesOption { }
5492
5593export interface UserConfig {
5694 /**
@@ -157,6 +195,8 @@ export async function resolveConfig(
157195 resetOutDir ( mainViteConfig , outDir , 'main' )
158196 }
159197
198+ const configDrivenPlugins : PluginOption [ ] = await resolveConfigDrivenPlugins ( mainViteConfig )
199+
160200 const builtInMainPlugins : PluginOption [ ] = [
161201 electronMainConfigPresetPlugin ( { root } ) ,
162202 electronMainConfigValidatorPlugin ( ) ,
@@ -165,13 +205,20 @@ export async function resolveConfig(
165205 modulePathPlugin (
166206 mergeConfig (
167207 {
168- plugins : [ electronMainConfigPresetPlugin ( { root } ) , assetPlugin ( ) , importMetaPlugin ( ) , esmShimPlugin ( ) ]
208+ plugins : [
209+ electronMainConfigPresetPlugin ( { root } ) ,
210+ assetPlugin ( ) ,
211+ importMetaPlugin ( ) ,
212+ esmShimPlugin ( ) ,
213+ ...configDrivenPlugins
214+ ]
169215 } ,
170216 mainViteConfig
171217 )
172218 ) ,
173219 importMetaPlugin ( ) ,
174- esmShimPlugin ( )
220+ esmShimPlugin ( ) ,
221+ ...configDrivenPlugins
175222 ]
176223
177224 mainViteConfig . plugins = builtInMainPlugins . concat ( mainViteConfig . plugins || [ ] )
@@ -188,12 +235,15 @@ export async function resolveConfig(
188235 resetOutDir ( preloadViteConfig , outDir , 'preload' )
189236 }
190237
238+ const configDrivenPlugins : PluginOption [ ] = await resolveConfigDrivenPlugins ( preloadViteConfig )
239+
191240 const builtInPreloadPlugins : PluginOption [ ] = [
192241 electronPreloadConfigPresetPlugin ( { root } ) ,
193242 electronPreloadConfigValidatorPlugin ( ) ,
194243 assetPlugin ( ) ,
195244 importMetaPlugin ( ) ,
196- esmShimPlugin ( )
245+ esmShimPlugin ( ) ,
246+ ...configDrivenPlugins
197247 ]
198248
199249 if ( preloadViteConfig . isolatedEntries ) {
@@ -205,7 +255,8 @@ export async function resolveConfig(
205255 electronPreloadConfigPresetPlugin ( { root } ) ,
206256 assetPlugin ( ) ,
207257 importMetaPlugin ( ) ,
208- esmShimPlugin ( )
258+ esmShimPlugin ( ) ,
259+ ...configDrivenPlugins
209260 ]
210261 } ,
211262 preloadViteConfig
@@ -278,6 +329,38 @@ function resetOutDir(config: ViteConfig, outDir: string, subOutDir: string): voi
278329 }
279330}
280331
332+ async function resolveConfigDrivenPlugins ( config : MainViteConfig | PreloadViteConfig ) : Promise < PluginOption [ ] > {
333+ const userPlugins = ( await asyncFlatten ( config . plugins || [ ] ) ) . filter ( Boolean ) as Plugin [ ]
334+
335+ const configDrivenPlugins : PluginOption [ ] = [ ]
336+
337+ const hasExternalizeDepsPlugin = userPlugins . some ( p => p . name === 'vite:externalize-deps' )
338+ if ( ! hasExternalizeDepsPlugin ) {
339+ const externalOptions = config . build ?. externalizeDeps ?? true
340+ if ( externalOptions ) {
341+ isOptions < ExternalOptions > ( externalOptions )
342+ ? configDrivenPlugins . push ( externalizeDepsPlugin ( externalOptions ) )
343+ : configDrivenPlugins . push ( externalizeDepsPlugin ( ) )
344+ }
345+ }
346+
347+ const hasBytecodePlugin = userPlugins . some ( p => p . name === 'vite:bytecode' )
348+ if ( ! hasBytecodePlugin ) {
349+ const bytecodeOptions = config . build ?. bytecode
350+ if ( bytecodeOptions ) {
351+ isOptions < BytecodeOptions > ( bytecodeOptions )
352+ ? configDrivenPlugins . push ( bytecodePlugin ( bytecodeOptions ) )
353+ : configDrivenPlugins . push ( bytecodePlugin ( ) )
354+ }
355+ }
356+
357+ return configDrivenPlugins
358+ }
359+
360+ function isOptions < T extends object > ( value : boolean | T ) : value is T {
361+ return typeof value === 'object' && value !== null
362+ }
363+
281364const CONFIG_FILE_NAME = 'electron.vite.config'
282365
283366export async function loadConfigFromFile (
0 commit comments