-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathvite.base.config.ts
More file actions
152 lines (137 loc) · 4.57 KB
/
vite.base.config.ts
File metadata and controls
152 lines (137 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { readFileSync } from 'node:fs'
import { builtinModules } from 'node:module'
import type { AddressInfo } from 'node:net'
import { join } from 'node:path'
import type { ConfigEnv, Plugin, UserConfig } from 'vite'
import pkg from './package.json'
export const builtins = [
'electron',
'bufferutil',
'utf-8-validate',
...builtinModules.map((m) => [m, `node:${m}`]).flat(),
]
// ESM-only packages (those with "type": "module" in their package.json)
// can't be loaded via CJS require(). In dev mode we externalize all deps
// for speed, so these must be detected and kept bundled by Vite instead.
function isEsmOnlyPackage(name: string): boolean {
try {
const pkgJsonPath = join(
process.cwd(),
'node_modules',
name,
'package.json'
)
const depPkg = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')) as {
type?: string
}
return depPkg.type === 'module'
} catch {
return false
}
}
// Dev: externalize all CJS-compatible deps for fast builds and HMR.
// Prod: only externalize builtins so Vite bundles everything — the
// forge Vite plugin strips node_modules from the ASAR, so externalized
// npm packages would be missing at runtime.
export function getExternal(command: 'serve' | 'build') {
if (command === 'build') return builtins
return [
...builtins,
...Object.keys(
'dependencies' in pkg ? (pkg.dependencies as Record<string, unknown>) : {}
).filter((dep) => !isEsmOnlyPackage(dep)),
]
}
export function getBuildConfig(env: ConfigEnv<'build'>): UserConfig {
const { root, mode, command } = env
return {
root,
mode,
build: {
// Prevent multiple builds from interfering with each other.
emptyOutDir: false,
// 🚧 Multiple builds may conflict.
outDir: '.vite/build',
watch: command === 'serve' ? {} : null,
minify: command === 'build',
sourcemap: command === 'serve',
// Match the Chromium in our Electron version. Legacy defaults (chrome87,
// …) make esbuild fail when transpiling modern deps (destructuring/rest).
target: 'esnext',
},
esbuild: {
target: 'esnext',
},
clearScreen: false,
}
}
export function getDefineKeys(names: string[]) {
const define: { [name: string]: K6StudioRuntimeKeys } = {}
return names.reduce((acc, name) => {
const NAME = name.toUpperCase()
const keys: K6StudioRuntimeKeys = {
VITE_DEV_SERVER_URL: `${NAME}_VITE_DEV_SERVER_URL`,
VITE_NAME: `${NAME}_VITE_NAME`,
SENTRY_DSN: 'SENTRY_DSN',
}
return { ...acc, [name]: keys }
}, define)
}
export function getBuildDefine(env: ConfigEnv<'build'>) {
const { command, forgeConfig } = env
const names = forgeConfig.renderer
.filter(({ name }) => name != null)
.map(({ name }) => name)
const defineKeys = getDefineKeys(names)
const define = Object.entries(defineKeys).reduce(
(acc, [name, keys]) => {
const { VITE_DEV_SERVER_URL, VITE_NAME, SENTRY_DSN } = keys
const def = {
[VITE_DEV_SERVER_URL]:
command === 'serve'
? JSON.stringify(process.env[VITE_DEV_SERVER_URL])
: undefined,
[VITE_NAME]: JSON.stringify(name),
[SENTRY_DSN]: JSON.stringify(process.env.SENTRY_DSN),
}
return { ...acc, ...def }
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{} as Record<string, any>
)
return define
}
export function pluginExposeRenderer(name: string): Plugin {
const { VITE_DEV_SERVER_URL } = getDefineKeys([name])[name]!
return {
name: '@electron-forge/plugin-vite:expose-renderer',
configureServer(server) {
process.viteDevServers ??= {}
// Expose server for preload scripts hot reload.
process.viteDevServers[name] = server
server.httpServer?.once('listening', () => {
const addressInfo = server.httpServer!.address() as AddressInfo
// Expose env constant for main process use.
process.env[VITE_DEV_SERVER_URL] =
`http://localhost:${addressInfo?.port}`
})
},
}
}
export function pluginHotRestart(command: 'reload' | 'restart'): Plugin {
return {
name: '@electron-forge/plugin-vite:hot-restart',
closeBundle() {
if (command === 'reload') {
for (const server of Object.values(process.viteDevServers)) {
// Preload scripts hot reload.
server.ws.send({ type: 'full-reload' })
}
} else {
// Main process hot restart.
// https://github.com/electron/forge/blob/v7.2.0/packages/api/core/src/api/start.ts#L216-L223
process.stdin.emit('data', 'rs')
}
},
}
}