mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-09 22:16:01 +08:00
refactor: split electron plugin into preset and validator plugins
This commit is contained in:
parent
0a79da03db
commit
88f6db2239
@ -7,7 +7,7 @@ import {
|
||||
type UserConfig as ViteConfig,
|
||||
type UserConfigExport as ViteConfigExport,
|
||||
type ConfigEnv,
|
||||
type Plugin,
|
||||
type PluginOption,
|
||||
type LogLevel,
|
||||
createLogger,
|
||||
mergeConfig,
|
||||
@ -15,7 +15,14 @@ import {
|
||||
} from 'vite'
|
||||
import { build } from 'esbuild'
|
||||
|
||||
import { electronMainVitePlugin, electronPreloadVitePlugin, electronRendererVitePlugin } from './plugins/electron'
|
||||
import {
|
||||
electronMainConfigPresetPlugin,
|
||||
electronMainConfigValidatorPlugin,
|
||||
electronPreloadConfigPresetPlugin,
|
||||
electronPreloadConfigValidatorPlugin,
|
||||
electronRendererConfigPresetPlugin,
|
||||
electronRendererConfigValidatorPlugin
|
||||
} from './plugins/electron'
|
||||
import assetPlugin from './plugins/asset'
|
||||
import workerPlugin from './plugins/worker'
|
||||
import importMetaPlugin from './plugins/importMeta'
|
||||
@ -173,21 +180,24 @@ export async function resolveConfig(
|
||||
resetOutDir(mainViteConfig, outDir, 'main')
|
||||
}
|
||||
|
||||
mergePlugins(mainViteConfig, [
|
||||
...electronMainVitePlugin({ root }),
|
||||
const builtInMainPlugins: PluginOption[] = [
|
||||
electronMainConfigPresetPlugin({ root }),
|
||||
electronMainConfigValidatorPlugin(),
|
||||
assetPlugin(),
|
||||
workerPlugin(),
|
||||
modulePathPlugin(
|
||||
mergeConfig(
|
||||
{
|
||||
plugins: [electronMainVitePlugin({ root })[0], assetPlugin(), importMetaPlugin(), esmShimPlugin()]
|
||||
plugins: [electronMainConfigPresetPlugin({ root }), assetPlugin(), importMetaPlugin(), esmShimPlugin()]
|
||||
},
|
||||
mainViteConfig
|
||||
)
|
||||
),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin()
|
||||
])
|
||||
]
|
||||
|
||||
mainViteConfig.plugins = builtInMainPlugins.concat(mainViteConfig.plugins || [])
|
||||
|
||||
loadResult.config.main = mainViteConfig
|
||||
loadResult.config.main.configFile = false
|
||||
@ -204,18 +214,22 @@ export async function resolveConfig(
|
||||
if (outDir) {
|
||||
resetOutDir(preloadViteConfig, outDir, 'preload')
|
||||
}
|
||||
mergePlugins(preloadViteConfig, [
|
||||
...electronPreloadVitePlugin({ root }),
|
||||
|
||||
const builtInPreloadPlugins: PluginOption[] = [
|
||||
electronPreloadConfigPresetPlugin({ root }),
|
||||
electronPreloadConfigValidatorPlugin(),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...(preloadViteConfig.isolatedEntries
|
||||
? [
|
||||
esmShimPlugin()
|
||||
]
|
||||
|
||||
if (preloadViteConfig.isolatedEntries) {
|
||||
builtInPreloadPlugins.push(
|
||||
isolateEntriesPlugin(
|
||||
mergeConfig(
|
||||
{
|
||||
plugins: [
|
||||
electronPreloadVitePlugin({ root })[0],
|
||||
electronPreloadConfigPresetPlugin({ root }),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin()
|
||||
@ -224,9 +238,10 @@ export async function resolveConfig(
|
||||
preloadViteConfig
|
||||
)
|
||||
)
|
||||
]
|
||||
: [])
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
preloadViteConfig.plugins = builtInPreloadPlugins.concat(preloadViteConfig.plugins)
|
||||
|
||||
loadResult.config.preload = preloadViteConfig
|
||||
loadResult.config.preload.configFile = false
|
||||
@ -244,21 +259,25 @@ export async function resolveConfig(
|
||||
resetOutDir(rendererViteConfig, outDir, 'renderer')
|
||||
}
|
||||
|
||||
mergePlugins(rendererViteConfig, [
|
||||
...electronRendererVitePlugin({ root }),
|
||||
...(rendererViteConfig.isolatedEntries
|
||||
? [
|
||||
const builtInRendererPlugins: PluginOption[] = [
|
||||
electronRendererConfigPresetPlugin({ root }),
|
||||
electronRendererConfigValidatorPlugin()
|
||||
]
|
||||
|
||||
if (rendererViteConfig.isolatedEntries) {
|
||||
builtInRendererPlugins.push(
|
||||
isolateEntriesPlugin(
|
||||
mergeConfig(
|
||||
{
|
||||
plugins: [electronRendererVitePlugin({ root })[0]]
|
||||
plugins: [electronRendererConfigPresetPlugin({ root })]
|
||||
},
|
||||
rendererViteConfig
|
||||
)
|
||||
)
|
||||
]
|
||||
: [])
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
rendererViteConfig.plugins = builtInRendererPlugins.concat(rendererViteConfig.plugins || [])
|
||||
|
||||
loadResult.config.renderer = rendererViteConfig
|
||||
loadResult.config.renderer.configFile = false
|
||||
@ -295,11 +314,6 @@ function resetOutDir(config: ViteConfig, outDir: string, subOutDir: string): voi
|
||||
}
|
||||
}
|
||||
|
||||
function mergePlugins(config: ViteConfig, plugins: Plugin[]): void {
|
||||
const userPlugins = config.plugins || []
|
||||
config.plugins = userPlugins.concat(plugins)
|
||||
}
|
||||
|
||||
const CONFIG_FILE_NAME = 'electron.vite.config'
|
||||
|
||||
export async function loadConfigFromFile(
|
||||
|
||||
@ -50,10 +50,9 @@ function resolveBuildOutputs(
|
||||
return outputs
|
||||
}
|
||||
|
||||
export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[] {
|
||||
return [
|
||||
{
|
||||
name: 'vite:electron-main-preset-config',
|
||||
export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-main-config-preset',
|
||||
apply: 'build',
|
||||
enforce: 'pre',
|
||||
config(config): void {
|
||||
@ -94,11 +93,7 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
|
||||
formats:
|
||||
libOptions && libOptions.formats && libOptions.formats.length > 0
|
||||
? []
|
||||
: [
|
||||
outputOptions && !Array.isArray(outputOptions) && outputOptions.format
|
||||
? outputOptions.format
|
||||
: format
|
||||
]
|
||||
: [outputOptions && !Array.isArray(outputOptions) && outputOptions.format ? outputOptions.format : format]
|
||||
}
|
||||
} else {
|
||||
defaultConfig.build.rollupOptions.output['format'] = format
|
||||
@ -129,9 +124,12 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
|
||||
config.build.ssrEmitAssets = true
|
||||
config.ssr = { ...config.ssr, ...{ noExternal: true } }
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'vite:electron-main-resolved-config',
|
||||
}
|
||||
}
|
||||
|
||||
export function electronMainConfigValidatorPlugin(): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-main-config-validator',
|
||||
apply: 'build',
|
||||
enforce: 'post',
|
||||
configResolved(config): void {
|
||||
@ -179,13 +177,11 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plugin[] {
|
||||
return [
|
||||
{
|
||||
name: 'vite:electron-preload-preset-config',
|
||||
export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOptions): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-preload-config-preset',
|
||||
apply: 'build',
|
||||
enforce: 'pre',
|
||||
config(config): void {
|
||||
@ -227,11 +223,7 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
|
||||
formats:
|
||||
libOptions && libOptions.formats && libOptions.formats.length > 0
|
||||
? []
|
||||
: [
|
||||
outputOptions && !Array.isArray(outputOptions) && outputOptions.format
|
||||
? outputOptions.format
|
||||
: format
|
||||
]
|
||||
: [outputOptions && !Array.isArray(outputOptions) && outputOptions.format ? outputOptions.format : format]
|
||||
}
|
||||
} else {
|
||||
defaultConfig.build.rollupOptions.output['format'] = format
|
||||
@ -281,9 +273,12 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
|
||||
config.ssr = mergeConfig(defaultConfig.ssr, config.ssr || {})
|
||||
config.ssr.noExternal = true
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'vite:electron-preload-resolved-config',
|
||||
}
|
||||
}
|
||||
|
||||
export function electronPreloadConfigValidatorPlugin(): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-preload-config-validator',
|
||||
apply: 'build',
|
||||
enforce: 'post',
|
||||
configResolved(config): void {
|
||||
@ -331,13 +326,11 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plugin[] {
|
||||
return [
|
||||
{
|
||||
name: 'vite:electron-renderer-preset-config',
|
||||
export function electronRendererConfigPresetPlugin(options?: ElectronPluginOptions): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-renderer-config-preset',
|
||||
enforce: 'pre',
|
||||
config(config): void {
|
||||
const root = options?.root || process.cwd()
|
||||
@ -385,9 +378,12 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu
|
||||
|
||||
config.envPrefix = config.envPrefix || ['RENDERER_VITE_', 'VITE_']
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'vite:electron-renderer-resolved-config',
|
||||
}
|
||||
}
|
||||
|
||||
export function electronRendererConfigValidatorPlugin(): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-renderer-config-validator',
|
||||
enforce: 'post',
|
||||
configResolved(config): void {
|
||||
if (config.base !== './' && config.base !== '/') {
|
||||
@ -413,5 +409,4 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user