Skip to content

Commit c9b230a

Browse files
committed
refactor(v0.4.0): better code logic
1 parent e4a8aef commit c9b230a

File tree

4 files changed

+62
-103
lines changed

4 files changed

+62
-103
lines changed

src/build.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
import { builtinModules } from 'module'
2-
import { build as viteBuild, mergeConfig } from 'vite'
3-
import type { ResolvedConfig, UserConfig } from 'vite'
2+
import { build as viteBuild, InlineConfig, mergeConfig } from 'vite'
3+
import type { ResolvedConfig } from 'vite'
44
import type { Configuration } from './types'
55

66
const isProduction = process.env./* from mode option */NODE_ENV === 'production'
77

88
export async function build(config: Configuration, viteConfig: ResolvedConfig) {
99
if (config.preload) {
10-
const preloadConfig = resolveBuildConfig(
11-
'preload',
12-
config,
13-
viteConfig,
14-
config.preload.vite,
15-
)
16-
await viteBuild({
17-
configFile: false,
18-
...preloadConfig,
19-
})
10+
await viteBuild(resolveBuildConfig('preload', config, viteConfig))
2011
}
21-
22-
const mainConfig = resolveBuildConfig(
23-
'main',
24-
config,
25-
viteConfig,
26-
config.main.vite,
27-
)
28-
await viteBuild({
29-
configFile: false,
30-
...mainConfig,
31-
})
12+
await viteBuild(resolveBuildConfig('main', config, viteConfig))
3213
}
3314

3415
// -------------------------------------------------
@@ -37,10 +18,13 @@ export function resolveBuildConfig(
3718
proc: 'main' | 'preload',
3819
config: Configuration,
3920
viteConfig: ResolvedConfig,
40-
overrides: Parameters<typeof mergeConfig>[1] = {},
41-
) {
42-
const conf: UserConfig = {
21+
): InlineConfig {
22+
const conf: InlineConfig = {
23+
// 🚧 Avoid recursive build caused by load config file
24+
configFile: false,
25+
envFile: false,
4326
publicDir: false,
27+
4428
build: {
4529
minify: isProduction,
4630
sourcemap: true,
@@ -79,5 +63,5 @@ export function resolveBuildConfig(
7963
// Assign default dir
8064
conf.build.outDir = `${viteConfig.build.outDir}/electron-${proc}`
8165

82-
return mergeConfig(conf, overrides) as UserConfig
66+
return mergeConfig(conf, config[proc]?.vite || {}) as InlineConfig
8367
}

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Configuration } from './types'
22
import type { Plugin } from 'vite'
3-
import renderer from '../renderer'
43
import { bootstrap } from './serve'
54
import { build } from './build'
65

@@ -30,6 +29,5 @@ export default function electron(config: Configuration): Plugin[] {
3029
await build(config, viteConfig)
3130
},
3231
},
33-
...(config.main.nodeIntegration ? renderer() : []),
3432
]
3533
}

src/serve.ts

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path'
22
import { spawn } from 'child_process'
33
import type { ChildProcessWithoutNullStreams } from 'child_process'
44
import type { AddressInfo } from 'net'
5-
import type { ViteDevServer, UserConfig } from 'vite'
5+
import type { ViteDevServer, UserConfig, InlineConfig } from 'vite'
66
import { build as viteBuild, mergeConfig } from 'vite'
77
import { resolveBuildConfig } from './build'
88
import type { Configuration } from './types'
@@ -12,31 +12,23 @@ export async function bootstrap(config: Configuration, server: ViteDevServer) {
1212
const { config: viteConfig } = server
1313

1414
if (config.preload) {
15-
const preloadConfig = resolveBuildConfig(
16-
'preload',
17-
config,
18-
viteConfig,
19-
mergeConfig(
20-
{
21-
mode: 'development',
22-
build: {
23-
watch: true,
15+
const preloadConfig = mergeConfig(
16+
resolveBuildConfig('preload', config, viteConfig),
17+
{
18+
mode: 'development',
19+
build: {
20+
watch: true,
21+
},
22+
plugins: [{
23+
name: 'electron-preload-watcher',
24+
writeBundle() {
25+
server.ws.send({ type: 'full-reload' })
2426
},
25-
plugins: [{
26-
name: 'electron-preload-watcher',
27-
writeBundle() {
28-
server.ws.send({ type: 'full-reload' })
29-
},
30-
}],
31-
} as UserConfig,
32-
config.preload.vite || {},
33-
)
34-
)
27+
}],
28+
} as UserConfig,
29+
) as InlineConfig
3530

36-
await viteBuild({
37-
configFile: false,
38-
...preloadConfig,
39-
})
31+
await viteBuild(preloadConfig)
4032
}
4133

4234
// ----------------------------------------------------------------
@@ -48,33 +40,26 @@ export async function bootstrap(config: Configuration, server: ViteDevServer) {
4840
VITE_DEV_SERVER_PORT: address.port,
4941
})
5042

51-
const mainConfig = resolveBuildConfig(
52-
'main',
53-
config,
54-
viteConfig,
55-
mergeConfig(
56-
{
57-
mode: 'development',
58-
build: {
59-
watch: true,
43+
const mainConfig = mergeConfig(
44+
resolveBuildConfig('main', config, viteConfig),
45+
{
46+
mode: 'development',
47+
build: {
48+
watch: true,
49+
},
50+
plugins: [{
51+
name: 'electron-main-watcher',
52+
writeBundle() {
53+
electronProcess && electronProcess.kill()
54+
55+
// TODO: Check if electronEntry is a directory
56+
let electronEntry = path.join(mainConfig.build.outDir, path.parse(config.main.entry).name)
57+
// Start Electron App
58+
electronProcess = spawn(electronPath, [electronEntry], { stdio: 'inherit', env })
6059
},
61-
plugins: [{
62-
name: 'electron-main-watcher',
63-
writeBundle() {
64-
electronProcess && electronProcess.kill()
60+
}],
61+
} as UserConfig,
62+
) as InlineConfig
6563

66-
// TODO: Check if electronEntry is a directory
67-
let electronEntry = path.join(mainConfig.build.outDir, path.parse(config.main.entry).name)
68-
// Start Electron App
69-
electronProcess = spawn(electronPath, [electronEntry], { stdio: 'inherit', env })
70-
},
71-
}],
72-
} as UserConfig,
73-
config.main.vite || {},
74-
)
75-
)
76-
await viteBuild({
77-
configFile: false,
78-
...mainConfig,
79-
})
64+
await viteBuild(mainConfig)
8065
}

src/types.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
1-
import type {
2-
LibraryOptions,
3-
UserConfig,
4-
} from 'vite'
1+
import type { LibraryOptions, UserConfig } from 'vite'
52
import type { InputOption } from 'rollup'
63

7-
export interface MainConfig {
8-
/**
9-
* Shortcut of `build.lib.entry`
10-
*/
11-
entry: LibraryOptions['entry']
12-
vite?: UserConfig
13-
nodeIntegration?: boolean
14-
}
15-
16-
export interface PreloadConfig {
17-
/**
18-
* Shortcut of `build.rollupOptions.input`
19-
*/
20-
input?: InputOption
21-
vite?: UserConfig
22-
}
23-
244
export interface Configuration {
25-
main: MainConfig
26-
preload?: PreloadConfig
5+
main: {
6+
/**
7+
* Shortcut of `build.lib.entry`
8+
*/
9+
entry: LibraryOptions['entry']
10+
vite?: UserConfig
11+
}
12+
preload?: {
13+
/**
14+
* Shortcut of `build.rollupOptions.input`
15+
*/
16+
input: InputOption
17+
vite?: UserConfig
18+
}
2719
}

0 commit comments

Comments
 (0)