mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-06-04 12:18:16 +08:00
Compare commits
6 Commits
v6.0.0-bet
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31965d2972 | ||
|
|
4bede21c81 | ||
|
|
31bfc4886b | ||
|
|
371b650d71 | ||
|
|
5333d5216a | ||
|
|
2765eea74d |
@ -1,3 +1,9 @@
|
|||||||
|
### v6.0.0-beta.0 (_2026-04-12_)
|
||||||
|
|
||||||
|
- refactor!: simplify resolve config and isolate user config for sub-builds
|
||||||
|
- fix!: compatible with rollupOptions and rolldownOptions
|
||||||
|
- fix: chunk filename is wrong in ES mode
|
||||||
|
|
||||||
### v6.0.0-beta.0 (_2026-04-09_)
|
### v6.0.0-beta.0 (_2026-04-09_)
|
||||||
|
|
||||||
- refactor: `?modulePath` sub-build output strategy
|
- refactor: `?modulePath` sub-build output strategy
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "electron-vite",
|
"name": "electron-vite",
|
||||||
"version": "6.0.0-beta.0",
|
"version": "6.0.0-beta.1",
|
||||||
"description": "Electron build tooling based on Vite",
|
"description": "Electron build tooling based on Vite",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
|
|||||||
225
src/config.ts
225
src/config.ts
@ -164,7 +164,8 @@ export async function resolveConfig(
|
|||||||
|
|
||||||
process.env.NODE_ENV = defaultMode
|
process.env.NODE_ENV = defaultMode
|
||||||
|
|
||||||
let userConfig: UserConfig | undefined
|
const userConfig: UserConfig | undefined = {}
|
||||||
|
|
||||||
let configFileDependencies: string[] = []
|
let configFileDependencies: string[] = []
|
||||||
|
|
||||||
let { configFile } = config
|
let { configFile } = config
|
||||||
@ -191,123 +192,20 @@ export async function resolveConfig(
|
|||||||
|
|
||||||
const outDir = config.build?.outDir
|
const outDir = config.build?.outDir
|
||||||
|
|
||||||
if (loadResult.config.main) {
|
const { main, preload, renderer } = loadResult.config
|
||||||
const mainViteConfig: MainViteConfig = mergeConfig(loadResult.config.main, deepClone(config))
|
|
||||||
|
|
||||||
mainViteConfig.mode = inlineConfig.mode || mainViteConfig.mode || defaultMode
|
if (main) {
|
||||||
|
userConfig.main = await new MainConfigFactory(main, config, { outDir, root }).build()
|
||||||
if (outDir) {
|
|
||||||
resetOutDir(mainViteConfig, outDir, 'main')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(mainViteConfig)
|
if (preload) {
|
||||||
|
userConfig.preload = await new PreloadConfigFactory(preload, config, { outDir, root }).build()
|
||||||
const builtInMainPlugins: PluginOption[] = [
|
|
||||||
electronMainConfigPresetPlugin({ root }),
|
|
||||||
electronMainConfigValidatorPlugin(),
|
|
||||||
assetPlugin(),
|
|
||||||
workerPlugin(),
|
|
||||||
modulePathPlugin(
|
|
||||||
mergeConfig(
|
|
||||||
{
|
|
||||||
plugins: [
|
|
||||||
electronMainConfigPresetPlugin({ root }),
|
|
||||||
assetPlugin(),
|
|
||||||
importMetaPlugin(),
|
|
||||||
esmShimPlugin(),
|
|
||||||
...configDrivenPlugins
|
|
||||||
]
|
|
||||||
},
|
|
||||||
mainViteConfig
|
|
||||||
)
|
|
||||||
),
|
|
||||||
importMetaPlugin(),
|
|
||||||
esmShimPlugin(),
|
|
||||||
...configDrivenPlugins
|
|
||||||
]
|
|
||||||
|
|
||||||
mainViteConfig.plugins = builtInMainPlugins.concat(mainViteConfig.plugins || [])
|
|
||||||
|
|
||||||
loadResult.config.main = mainViteConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadResult.config.preload) {
|
if (renderer) {
|
||||||
const preloadViteConfig: PreloadViteConfig = mergeConfig(loadResult.config.preload, deepClone(config))
|
userConfig.renderer = await new RendererConfigFactory(renderer, config, { outDir, root }).build()
|
||||||
|
|
||||||
preloadViteConfig.mode = inlineConfig.mode || preloadViteConfig.mode || defaultMode
|
|
||||||
|
|
||||||
if (outDir) {
|
|
||||||
resetOutDir(preloadViteConfig, outDir, 'preload')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(preloadViteConfig)
|
|
||||||
|
|
||||||
const builtInPreloadPlugins: PluginOption[] = [
|
|
||||||
electronPreloadConfigPresetPlugin({ root }),
|
|
||||||
electronPreloadConfigValidatorPlugin(),
|
|
||||||
assetPlugin(),
|
|
||||||
importMetaPlugin(),
|
|
||||||
esmShimPlugin(),
|
|
||||||
...configDrivenPlugins
|
|
||||||
]
|
|
||||||
|
|
||||||
if (preloadViteConfig.build?.isolatedEntries) {
|
|
||||||
builtInPreloadPlugins.push(
|
|
||||||
isolateEntriesPlugin(
|
|
||||||
mergeConfig(
|
|
||||||
{
|
|
||||||
plugins: [
|
|
||||||
electronPreloadConfigPresetPlugin({ root }),
|
|
||||||
assetPlugin(),
|
|
||||||
importMetaPlugin(),
|
|
||||||
esmShimPlugin(),
|
|
||||||
...configDrivenPlugins
|
|
||||||
]
|
|
||||||
},
|
|
||||||
preloadViteConfig
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
preloadViteConfig.plugins = builtInPreloadPlugins.concat(preloadViteConfig.plugins)
|
|
||||||
|
|
||||||
loadResult.config.preload = preloadViteConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
if (loadResult.config.renderer) {
|
|
||||||
const rendererViteConfig: RendererViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config))
|
|
||||||
|
|
||||||
rendererViteConfig.mode = inlineConfig.mode || rendererViteConfig.mode || defaultMode
|
|
||||||
|
|
||||||
if (outDir) {
|
|
||||||
resetOutDir(rendererViteConfig, outDir, 'renderer')
|
|
||||||
}
|
|
||||||
|
|
||||||
const builtInRendererPlugins: PluginOption[] = [
|
|
||||||
electronRendererConfigPresetPlugin({ root }),
|
|
||||||
electronRendererConfigValidatorPlugin()
|
|
||||||
]
|
|
||||||
|
|
||||||
if (rendererViteConfig.build?.isolatedEntries) {
|
|
||||||
builtInRendererPlugins.push(
|
|
||||||
isolateEntriesPlugin(
|
|
||||||
mergeConfig(
|
|
||||||
{
|
|
||||||
plugins: [electronRendererConfigPresetPlugin({ root })]
|
|
||||||
},
|
|
||||||
rendererViteConfig
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
rendererViteConfig.plugins = builtInRendererPlugins.concat(rendererViteConfig.plugins || [])
|
|
||||||
|
|
||||||
loadResult.config.renderer = rendererViteConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
userConfig = loadResult.config
|
|
||||||
configFile = loadResult.path
|
configFile = loadResult.path
|
||||||
configFileDependencies = loadResult.dependencies
|
configFileDependencies = loadResult.dependencies
|
||||||
}
|
}
|
||||||
@ -322,6 +220,111 @@ export async function resolveConfig(
|
|||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export abstract class ConfigFactory<T extends MainViteConfig | PreloadViteConfig | RendererViteConfig> {
|
||||||
|
constructor(
|
||||||
|
protected readonly baseConfig: T,
|
||||||
|
protected readonly inlineConfig: InlineConfig,
|
||||||
|
protected readonly options: { outDir?: string; root?: string }
|
||||||
|
) {
|
||||||
|
baseConfig.build ??= {}
|
||||||
|
baseConfig.build.rolldownOptions ??= baseConfig.build.rollupOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
async build(cleanMode?: boolean): Promise<T> {
|
||||||
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||||
|
const config = mergeConfig(deepClone(this.baseConfig) as any, deepClone(this.inlineConfig)) as T
|
||||||
|
|
||||||
|
config.mode = this.inlineConfig.mode || config.mode || process.env.NODE_ENV
|
||||||
|
|
||||||
|
if (this.options.outDir) {
|
||||||
|
resetOutDir(config, this.options.outDir, this.processType())
|
||||||
|
}
|
||||||
|
|
||||||
|
const builtinPlugins = await this.resolveBuiltinPlugins(config, cleanMode)
|
||||||
|
|
||||||
|
config.plugins = builtinPlugins.concat(config.plugins || [])
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract processType(): 'main' | 'preload' | 'renderer'
|
||||||
|
|
||||||
|
protected abstract resolveBuiltinPlugins(config: T, cleanMode?: boolean): Promise<PluginOption[]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MainConfigFactory extends ConfigFactory<MainViteConfig> {
|
||||||
|
protected processType(): 'main' {
|
||||||
|
return 'main'
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async resolveBuiltinPlugins(config: MainViteConfig, cleanMode?: boolean): Promise<PluginOption[]> {
|
||||||
|
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(config)
|
||||||
|
|
||||||
|
return cleanMode
|
||||||
|
? [
|
||||||
|
electronMainConfigPresetPlugin({ root: this.options.root }),
|
||||||
|
assetPlugin(),
|
||||||
|
importMetaPlugin(),
|
||||||
|
esmShimPlugin(),
|
||||||
|
...configDrivenPlugins
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
electronMainConfigPresetPlugin({ root: this.options.root }),
|
||||||
|
electronMainConfigValidatorPlugin(),
|
||||||
|
assetPlugin(),
|
||||||
|
workerPlugin(),
|
||||||
|
modulePathPlugin(this),
|
||||||
|
importMetaPlugin(),
|
||||||
|
esmShimPlugin(),
|
||||||
|
...configDrivenPlugins
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PreloadConfigFactory extends ConfigFactory<PreloadViteConfig> {
|
||||||
|
protected processType(): 'preload' {
|
||||||
|
return 'preload'
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async resolveBuiltinPlugins(config: PreloadViteConfig, cleanMode?: boolean): Promise<PluginOption[]> {
|
||||||
|
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(config)
|
||||||
|
|
||||||
|
return cleanMode
|
||||||
|
? [
|
||||||
|
electronPreloadConfigPresetPlugin({ root: this.options.root }),
|
||||||
|
assetPlugin(),
|
||||||
|
importMetaPlugin(),
|
||||||
|
esmShimPlugin(),
|
||||||
|
...configDrivenPlugins
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
electronPreloadConfigPresetPlugin({ root: this.options.root }),
|
||||||
|
electronPreloadConfigValidatorPlugin(),
|
||||||
|
assetPlugin(),
|
||||||
|
importMetaPlugin(),
|
||||||
|
esmShimPlugin(),
|
||||||
|
...configDrivenPlugins,
|
||||||
|
...(config.build?.isolatedEntries ? [isolateEntriesPlugin(this)] : [])
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RendererConfigFactory extends ConfigFactory<RendererViteConfig> {
|
||||||
|
protected processType(): 'renderer' {
|
||||||
|
return 'renderer'
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async resolveBuiltinPlugins(config: RendererViteConfig, cleanMode?: boolean): Promise<PluginOption[]> {
|
||||||
|
return cleanMode
|
||||||
|
? [electronRendererConfigPresetPlugin({ root: this.options.root })]
|
||||||
|
: [
|
||||||
|
electronRendererConfigPresetPlugin({ root: this.options.root }),
|
||||||
|
electronRendererConfigValidatorPlugin(),
|
||||||
|
...(config.build?.isolatedEntries ? [isolateEntriesPlugin(this)] : [])
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function resetOutDir(config: ViteConfig, outDir: string, subOutDir: string): void {
|
function resetOutDir(config: ViteConfig, outDir: string, subOutDir: string): void {
|
||||||
let userOutDir = config.build?.outDir
|
let userOutDir = config.build?.outDir
|
||||||
if (outDir === userOutDir) {
|
if (outDir === userOutDir) {
|
||||||
|
|||||||
@ -205,7 +205,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
const build = config.build
|
const build = config.build
|
||||||
const resolvedOutputs = resolveBuildOutputs(build.rollupOptions.output, build.lib)
|
const resolvedOutputs = resolveBuildOutputs(build.rolldownOptions.output, build.lib)
|
||||||
if (resolvedOutputs) {
|
if (resolvedOutputs) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||||
const output = outputs[0]
|
const output = outputs[0]
|
||||||
@ -213,7 +213,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
config.logger.warn(
|
config.logger.warn(
|
||||||
colors.yellow(
|
colors.yellow(
|
||||||
'bytecodePlugin does not support ES module, please remove "type": "module" ' +
|
'bytecodePlugin does not support ES module, please remove "type": "module" ' +
|
||||||
'in package.json or set the "build.rollupOptions.output.format" option to "cjs".'
|
'in package.json or set build.rollupOptions.output.format (or build.rolldownOptions.output.format) to "cjs".'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import path from 'node:path'
|
|||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { builtinModules } from 'node:module'
|
import { builtinModules } from 'node:module'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { type Plugin, type LibraryOptions, type Rolldown, mergeConfig, normalizePath } from 'vite'
|
import { type Plugin, type LibraryOptions, type Rolldown, type UserConfig, mergeConfig, normalizePath } from 'vite'
|
||||||
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
||||||
import { loadPackageData } from '../utils'
|
import { loadPackageData } from '../utils'
|
||||||
|
|
||||||
@ -64,29 +64,24 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
|
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
|
||||||
|
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
resolve: {
|
|
||||||
browserField: false,
|
|
||||||
mainFields: ['module', 'jsnext:main', 'jsnext'],
|
|
||||||
conditions: ['node']
|
|
||||||
},
|
|
||||||
build: {
|
build: {
|
||||||
outDir: path.resolve(root, 'out', 'main'),
|
outDir: path.resolve(root, 'out', 'main'),
|
||||||
target: nodeTarget,
|
target: nodeTarget,
|
||||||
assetsDir: 'chunks',
|
assetsDir: 'chunks',
|
||||||
rollupOptions: {
|
rolldownOptions: {
|
||||||
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])],
|
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])],
|
||||||
output: {}
|
output: {}
|
||||||
},
|
},
|
||||||
reportCompressedSize: false,
|
reportCompressedSize: false,
|
||||||
minify: false
|
minify: false
|
||||||
}
|
}
|
||||||
}
|
} satisfies UserConfig
|
||||||
|
|
||||||
const build = config.build || {}
|
const build = config.build || {}
|
||||||
const rollupOptions = build.rollupOptions || {}
|
const rolldownOptions = build.rolldownOptions || {}
|
||||||
if (!rollupOptions.input) {
|
if (!rolldownOptions.input) {
|
||||||
const libOptions = build.lib
|
const libOptions = build.lib
|
||||||
const outputOptions = rollupOptions.output
|
const outputOptions = rolldownOptions.output
|
||||||
defaultConfig.build['lib'] = {
|
defaultConfig.build['lib'] = {
|
||||||
entry: findLibEntry(root, 'main'),
|
entry: findLibEntry(root, 'main'),
|
||||||
formats:
|
formats:
|
||||||
@ -95,10 +90,10 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
: [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.rolldownOptions.output['format'] = format
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join(
|
defaultConfig.build.rolldownOptions.output['assetFileNames'] = path.posix.join(
|
||||||
build.assetsDir || defaultConfig.build.assetsDir,
|
build.assetsDir || defaultConfig.build.assetsDir,
|
||||||
'[name]-[hash].[ext]'
|
'[name]-[hash].[ext]'
|
||||||
)
|
)
|
||||||
@ -106,8 +101,6 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
const buildConfig = mergeConfig(defaultConfig.build, build)
|
const buildConfig = mergeConfig(defaultConfig.build, build)
|
||||||
config.build = buildConfig
|
config.build = buildConfig
|
||||||
|
|
||||||
config.resolve = mergeConfig(defaultConfig.resolve, config.resolve || {})
|
|
||||||
|
|
||||||
config.define = config.define || {}
|
config.define = config.define || {}
|
||||||
config.define = { ...processEnvDefine(), ...config.define }
|
config.define = { ...processEnvDefine(), ...config.define }
|
||||||
|
|
||||||
@ -121,8 +114,9 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
|||||||
// enable ssr build
|
// enable ssr build
|
||||||
config.build.ssr = true
|
config.build.ssr = true
|
||||||
config.build.ssrEmitAssets = true
|
config.build.ssrEmitAssets = true
|
||||||
config.build.rolldownOptions = config.build.rollupOptions
|
|
||||||
config.ssr = { ...config.ssr, ...{ noExternal: true } }
|
config.ssr = { ...config.ssr, ...{ noExternal: true } }
|
||||||
|
|
||||||
|
config.build.rollupOptions = config.build.rolldownOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,16 +138,17 @@ export function electronMainConfigValidatorPlugin(): Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const libOptions = build.lib
|
const libOptions = build.lib
|
||||||
const rollupOptions = build.rollupOptions
|
const rolldownOptions = build.rolldownOptions
|
||||||
|
|
||||||
if (!(libOptions && libOptions.entry) && !rollupOptions?.input) {
|
if (!(libOptions && libOptions.entry) && !rolldownOptions?.input) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'An entry point is required in the electron vite main config, ' +
|
'An entry point is required in the electron vite main config, ' +
|
||||||
'which can be specified using "build.lib.entry" or "build.rollupOptions.input".'
|
'which can be specified using build.lib.entry, build.rollupOptions.input, ' +
|
||||||
|
'or build.rolldownOptions.input.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedOutputs = resolveBuildOutputs(rollupOptions.output, libOptions)
|
const resolvedOutputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
||||||
|
|
||||||
if (resolvedOutputs) {
|
if (resolvedOutputs) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||||
@ -204,20 +199,20 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
outDir: path.resolve(root, 'out', 'preload'),
|
outDir: path.resolve(root, 'out', 'preload'),
|
||||||
target: nodeTarget,
|
target: nodeTarget,
|
||||||
assetsDir: 'chunks',
|
assetsDir: 'chunks',
|
||||||
rollupOptions: {
|
rolldownOptions: {
|
||||||
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])],
|
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])],
|
||||||
output: {}
|
output: {}
|
||||||
},
|
},
|
||||||
reportCompressedSize: false,
|
reportCompressedSize: false,
|
||||||
minify: false
|
minify: false
|
||||||
}
|
}
|
||||||
}
|
} satisfies UserConfig
|
||||||
|
|
||||||
const build = config.build || {}
|
const build = config.build || {}
|
||||||
const rollupOptions = build.rollupOptions || {}
|
const rolldownOptions = build.rolldownOptions || {}
|
||||||
if (!rollupOptions.input) {
|
if (!rolldownOptions.input) {
|
||||||
const libOptions = build.lib
|
const libOptions = build.lib
|
||||||
const outputOptions = rollupOptions.output
|
const outputOptions = rolldownOptions.output
|
||||||
defaultConfig.build['lib'] = {
|
defaultConfig.build['lib'] = {
|
||||||
entry: findLibEntry(root, 'preload'),
|
entry: findLibEntry(root, 'preload'),
|
||||||
formats:
|
formats:
|
||||||
@ -226,10 +221,10 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
: [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.rolldownOptions.output['format'] = format
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join(
|
defaultConfig.build.rolldownOptions.output['assetFileNames'] = path.posix.join(
|
||||||
build.assetsDir || defaultConfig.build.assetsDir,
|
build.assetsDir || defaultConfig.build.assetsDir,
|
||||||
'[name]-[hash].[ext]'
|
'[name]-[hash].[ext]'
|
||||||
)
|
)
|
||||||
@ -237,22 +232,24 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
const buildConfig = mergeConfig(defaultConfig.build, build)
|
const buildConfig = mergeConfig(defaultConfig.build, build)
|
||||||
config.build = buildConfig
|
config.build = buildConfig
|
||||||
|
|
||||||
const resolvedOutputs = resolveBuildOutputs(config.build.rollupOptions!.output, config.build.lib || false)
|
const resolvedOutputs = resolveBuildOutputs(config.build.rolldownOptions!.output, config.build.lib || false)
|
||||||
|
|
||||||
if (resolvedOutputs) {
|
if (resolvedOutputs) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||||
|
|
||||||
if (outputs.find(({ format }) => format === 'es')) {
|
if (outputs.find(({ format }) => format === 'es')) {
|
||||||
if (Array.isArray(config.build.rollupOptions!.output)) {
|
if (Array.isArray(config.build.rolldownOptions!.output)) {
|
||||||
config.build.rollupOptions!.output.forEach(output => {
|
config.build.rolldownOptions!.output.forEach(output => {
|
||||||
if (output.format === 'es') {
|
if (output.format === 'es') {
|
||||||
output['entryFileNames'] = '[name].mjs'
|
output['entryFileNames'] = '[name].mjs'
|
||||||
output['chunkFileNames'] = '[name]-[hash].mjs'
|
output['chunkFileNames'] = '[name]-[hash].mjs'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
config.build.rollupOptions!.output!['entryFileNames'] = '[name].mjs'
|
config.build.rolldownOptions!.output!['entryFileNames'] = '[name].mjs'
|
||||||
config.build.rollupOptions!.output!['chunkFileNames'] = '[name]-[hash].mjs'
|
config.build.rolldownOptions!.output!['chunkFileNames'] = config.build.lib
|
||||||
|
? '[name]-[hash].mjs'
|
||||||
|
: path.posix.join(build.assetsDir || defaultConfig.build.assetsDir, '[name]-[hash].mjs')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -270,9 +267,10 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
// enable ssr build
|
// enable ssr build
|
||||||
config.build.ssr = true
|
config.build.ssr = true
|
||||||
config.build.ssrEmitAssets = true
|
config.build.ssrEmitAssets = true
|
||||||
config.build.rolldownOptions = config.build.rollupOptions
|
|
||||||
config.ssr = mergeConfig(defaultConfig.ssr, config.ssr || {})
|
config.ssr = mergeConfig(defaultConfig.ssr, config.ssr || {})
|
||||||
config.ssr.noExternal = true
|
config.ssr.noExternal = true
|
||||||
|
|
||||||
|
config.build.rollupOptions = config.build.rolldownOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -294,16 +292,17 @@ export function electronPreloadConfigValidatorPlugin(): Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const libOptions = build.lib
|
const libOptions = build.lib
|
||||||
const rollupOptions = build.rollupOptions
|
const rolldownOptions = build.rolldownOptions
|
||||||
|
|
||||||
if (!(libOptions && libOptions.entry) && !rollupOptions?.input) {
|
if (!(libOptions && libOptions.entry) && !rolldownOptions?.input) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'An entry point is required in the electron vite preload config, ' +
|
'An entry point is required in the electron vite preload config, ' +
|
||||||
'which can be specified using "build.lib.entry" or "build.rollupOptions.input".'
|
'which can be specified using build.lib.entry, build.rollupOptions.input, ' +
|
||||||
|
' or build.rolldownOptions.input.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedOutputs = resolveBuildOutputs(rollupOptions.output, libOptions)
|
const resolvedOutputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
||||||
|
|
||||||
if (resolvedOutputs) {
|
if (resolvedOutputs) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||||
@ -359,14 +358,14 @@ export function electronRendererConfigPresetPlugin(options?: ElectronPluginOptio
|
|||||||
outDir: path.resolve(root, 'out', 'renderer'),
|
outDir: path.resolve(root, 'out', 'renderer'),
|
||||||
target: chromeTarget,
|
target: chromeTarget,
|
||||||
modulePreload: { polyfill: false },
|
modulePreload: { polyfill: false },
|
||||||
rollupOptions: {
|
rolldownOptions: {
|
||||||
input: findInput(root)
|
input: findInput(root)
|
||||||
},
|
},
|
||||||
reportCompressedSize: false,
|
reportCompressedSize: false,
|
||||||
minify: false,
|
minify: false,
|
||||||
emptyOutDir: emptyOutDir()
|
emptyOutDir: emptyOutDir()
|
||||||
}
|
}
|
||||||
}
|
} satisfies UserConfig
|
||||||
|
|
||||||
if (config.build?.outDir) {
|
if (config.build?.outDir) {
|
||||||
config.build.outDir = path.resolve(root, config.build.outDir)
|
config.build.outDir = path.resolve(root, config.build.outDir)
|
||||||
@ -378,6 +377,8 @@ export function electronRendererConfigPresetPlugin(options?: ElectronPluginOptio
|
|||||||
config.envDir = config.envDir || path.resolve(root)
|
config.envDir = config.envDir || path.resolve(root)
|
||||||
|
|
||||||
config.envPrefix = config.envPrefix || ['RENDERER_VITE_', 'VITE_']
|
config.envPrefix = config.envPrefix || ['RENDERER_VITE_', 'VITE_']
|
||||||
|
|
||||||
|
config.build.rollupOptions = config.build.rolldownOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -403,10 +404,12 @@ export function electronRendererConfigValidatorPlugin(): Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const rollupOptions = build.rollupOptions
|
const rolldownOptions = build.rolldownOptions
|
||||||
if (!rollupOptions.input) {
|
if (!rolldownOptions.input) {
|
||||||
config.logger.warn(colors.yellow(`index.html file is not found in ${colors.dim('/src/renderer')} directory.`))
|
config.logger.warn(colors.yellow(`index.html file is not found in ${colors.dim('/src/renderer')} directory.`))
|
||||||
throw new Error('build.rollupOptions.input option is required in the electron vite renderer config.')
|
throw new Error(
|
||||||
|
'build.rollupOptions.input or build.rolldownOptions.input option is required in the electron vite renderer config.'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,13 +33,15 @@ export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | n
|
|||||||
config(config): void {
|
config(config): void {
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
build: {
|
build: {
|
||||||
rollupOptions: {
|
rolldownOptions: {
|
||||||
external: deps.length > 0 ? [...deps, new RegExp(`^(${deps.join('|')})/.+`)] : []
|
external: deps.length > 0 ? [...deps, new RegExp(`^(${deps.join('|')})/.+`)] : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const buildConfig = mergeConfig(defaultConfig.build, config.build || {})
|
const buildConfig = mergeConfig(defaultConfig.build, config.build || {})
|
||||||
config.build = buildConfig
|
config.build = buildConfig
|
||||||
|
|
||||||
|
config.build.rollupOptions = config.build.rolldownOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import path from 'node:path'
|
|||||||
import { type InlineConfig, type Plugin, type LogLevel, type Rolldown, build as viteBuild, mergeConfig } from 'vite'
|
import { type InlineConfig, type Plugin, type LogLevel, type Rolldown, build as viteBuild, mergeConfig } from 'vite'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { cleanUrl } from '../utils'
|
import { cleanUrl } from '../utils'
|
||||||
|
import type { ConfigFactory, PreloadViteConfig, RendererViteConfig } from '../config'
|
||||||
|
|
||||||
const VIRTUAL_ENTRY_ID = '\0virtual:isolate-entries'
|
const VIRTUAL_ENTRY_ID = '\0virtual:isolate-entries'
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ const LogLevels: Record<LogLevel, number> = {
|
|||||||
info: 3
|
info: 3
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteConfig | RendererViteConfig>): Plugin {
|
||||||
let entries: string[] | { [x: string]: string }[]
|
let entries: string[] | { [x: string]: string }[]
|
||||||
|
|
||||||
let transformedCount = 0
|
let transformedCount = 0
|
||||||
@ -49,6 +50,7 @@ export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
|||||||
|
|
||||||
async load(id): Promise<string | void> {
|
async load(id): Promise<string | void> {
|
||||||
if (id === VIRTUAL_ENTRY_ID) {
|
if (id === VIRTUAL_ENTRY_ID) {
|
||||||
|
const userConfig = await factory.build(true)
|
||||||
const shouldLog = LogLevels[userConfig.logLevel || 'info'] >= LogLevels.info
|
const shouldLog = LogLevels[userConfig.logLevel || 'info'] >= LogLevels.info
|
||||||
|
|
||||||
const watchFiles = new Set<string>()
|
const watchFiles = new Set<string>()
|
||||||
@ -132,7 +134,7 @@ async function bundleEntryFile(
|
|||||||
}) as InlineConfig
|
}) as InlineConfig
|
||||||
|
|
||||||
// rewrite the input instead of merging
|
// rewrite the input instead of merging
|
||||||
viteConfig.build!.rollupOptions!.input = input
|
viteConfig.build!.rolldownOptions!.input = input
|
||||||
|
|
||||||
const bundles = (await viteBuild(viteConfig)) as Rolldown.RolldownOutput
|
const bundles = (await viteBuild(viteConfig)) as Rolldown.RolldownOutput
|
||||||
|
|
||||||
|
|||||||
@ -2,13 +2,14 @@ import { type Plugin, type InlineConfig, type Rolldown, build as viteBuild, merg
|
|||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import { cleanUrl, toRelativePath } from '../utils'
|
import { cleanUrl, toRelativePath } from '../utils'
|
||||||
import { supportImportMetaPaths } from '../electron'
|
import { supportImportMetaPaths } from '../electron'
|
||||||
|
import type { ConfigFactory, MainViteConfig } from '../config'
|
||||||
|
|
||||||
const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
|
const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve `?modulePath` import and return the module bundle path.
|
* Resolve `?modulePath` import and return the module bundle path.
|
||||||
*/
|
*/
|
||||||
export default function modulePathPlugin(config: InlineConfig): Plugin {
|
export default function modulePathPlugin(factory: ConfigFactory<MainViteConfig>): Plugin {
|
||||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||||
const assetCache = new Set<string>()
|
const assetCache = new Set<string>()
|
||||||
return {
|
return {
|
||||||
@ -21,6 +22,7 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
|
|||||||
async load(id): Promise<string | void> {
|
async load(id): Promise<string | void> {
|
||||||
if (id.endsWith('?modulePath')) {
|
if (id.endsWith('?modulePath')) {
|
||||||
// id resolved by Vite resolve plugin
|
// id resolved by Vite resolve plugin
|
||||||
|
const config = await factory.build(true)
|
||||||
const bundles = await bundleEntryFile(cleanUrl(id), config)
|
const bundles = await bundleEntryFile(cleanUrl(id), config)
|
||||||
const [outputChunk, ...outputChunks] = bundles.output
|
const [outputChunk, ...outputChunks] = bundles.output
|
||||||
const hash = this.emitFile({
|
const hash = this.emitFile({
|
||||||
@ -113,8 +115,8 @@ async function bundleEntryFile(input: string, config: InlineConfig): Promise<Rol
|
|||||||
|
|
||||||
// rewrite the input instead of merging
|
// rewrite the input instead of merging
|
||||||
const buildOptions = viteConfig.build!
|
const buildOptions = viteConfig.build!
|
||||||
buildOptions.rollupOptions = {
|
buildOptions.rolldownOptions = {
|
||||||
...buildOptions.rollupOptions,
|
...buildOptions.rolldownOptions,
|
||||||
input
|
input
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user