Skip to content

Commit 08be346

Browse files
committed
feat(config): add build.externalizeDeps and build.bytecode config options to replace externalizeDepsPlugin and bytecodePlugin
1 parent 3e9ded6 commit 08be346

2 files changed

Lines changed: 100 additions & 8 deletions

File tree

src/config.ts

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
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'
2830
import esmShimPlugin from './plugins/esmShim'
2931
import modulePathPlugin from './plugins/modulePath'
3032
import 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

3337
export { 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

5593
export 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+
281364
const CONFIG_FILE_NAME = 'electron.vite.config'
282365

283366
export async function loadConfigFromFile(

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,12 @@ export function deepClone<T>(value: T): DeepWritable<T> {
111111
}
112112
return value as DeepWritable<T>
113113
}
114+
115+
type AsyncFlatten<T extends unknown[]> = T extends (infer U)[] ? Exclude<Awaited<U>, U[]>[] : never
116+
117+
export async function asyncFlatten<T extends unknown[]>(arr: T): Promise<AsyncFlatten<T>> {
118+
do {
119+
arr = (await Promise.all(arr)).flat(Infinity) as any
120+
} while (arr.some((v: any) => v?.then))
121+
return arr as unknown[] as AsyncFlatten<T>
122+
}

0 commit comments

Comments
 (0)