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