Skip to content

Commit 9682036

Browse files
refactor(multi-env): remove createBuilder from build mode, require createBuilder+buildApp in tests
Agent-Logs-Url: https://github.com/electron-vite/vite-plugin-electron/sessions/df671b54-d1ab-4460-9d3a-dc7a9a3fcf49 Co-authored-by: subframe7536 <78338239+subframe7536@users.noreply.github.com>
1 parent c1fe757 commit 9682036

2 files changed

Lines changed: 19 additions & 39 deletions

File tree

src/multi-env.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ export default function electron(
116116
}),
117117
)
118118

119-
// Set to true when buildApp takes ownership of the electron builds so that
120-
// the closeBundle-based fallback (used by the programmatic build() API) skips
121-
// them and avoids a double-build.
122-
let electronBuiltViaApp = false
123-
124119
// Build each electron environment from the given builder in declaration order.
125120
const buildElectronEnvironments = async (builder: ViteBuilder): Promise<void> => {
126121
for (const name of envNames) {
@@ -153,10 +148,6 @@ export default function electron(
153148
const prevBuildApp = config.builder?.buildApp
154149
config.builder ??= {}
155150
config.builder.buildApp = async (builder) => {
156-
// Claim ownership of the electron builds up-front so that the
157-
// closeBundle-based fallback (below) does not run them a second time.
158-
electronBuiltViaApp = true
159-
160151
if (prevBuildApp) {
161152
// Delegate all builds (renderer + any user-defined environments) to
162153
// the existing handler, then append the electron builds. The user's
@@ -225,27 +216,8 @@ export default function electron(
225216

226217
await buildElectronEnvironments(builder)
227218
},
228-
// Fallback for Vite's programmatic build() API, which skips buildApp and
229-
// only builds the first (client) environment via closeBundle. When the
230-
// CLI-driven buildApp path already ran, this is a no-op.
231-
async build(userConfig, configEnv) {
232-
if (electronBuiltViaApp || optionsArray.length === 0) {
233-
return
234-
}
235-
236-
const envsCfg = withExternalBuiltins({ environments: createEnvironments() })
237-
const builder = await createBuilder({
238-
configFile: false,
239-
publicDir: false,
240-
mode: configEnv.mode,
241-
root: userConfig.root,
242-
envDir: userConfig.envDir,
243-
envPrefix: userConfig.envPrefix,
244-
environments: envsCfg.environments,
245-
})
246-
247-
await buildElectronEnvironments(builder)
248-
},
219+
// Build is fully handled by the config() hook and builder.buildApp above.
220+
async build() {},
249221
}),
250222
]
251223
}

test/multi-env.test.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33

4-
import { build } from 'vite'
4+
import { createBuilder } from 'vite'
55
import type { Plugin } from 'vite'
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
77

@@ -147,7 +147,7 @@ describe('src/multi-env', () => {
147147

148148
expect(fs.existsSync(mockHtmlPath)).toBe(false)
149149

150-
await build({
150+
const builder = await createBuilder({
151151
configFile: false,
152152
root: mockHtmlRoot,
153153
build: {
@@ -158,6 +158,7 @@ describe('src/multi-env', () => {
158158
plugins: electron([]),
159159
logLevel: 'silent',
160160
})
161+
await builder.buildApp()
161162

162163
expect(fs.existsSync(mockHtmlPath)).toBe(false)
163164
expect(fs.existsSync(mockHtmlDistPath)).toBe(false)
@@ -202,7 +203,7 @@ describe('src/multi-env', () => {
202203

203204
await resetDirs(appOutDir, electronOutDir)
204205

205-
await build({
206+
const builder = await createBuilder({
206207
configFile: false,
207208
root,
208209
build: {
@@ -227,6 +228,7 @@ describe('src/multi-env', () => {
227228
],
228229
logLevel: 'silent',
229230
})
231+
await builder.buildApp()
230232

231233
expect(fs.existsSync(path.join(appOutDir, 'index.html'))).toBe(true)
232234
expect(fs.existsSync(path.join(electronOutDir, 'main.js'))).toBe(true)
@@ -247,7 +249,7 @@ describe('src/multi-env', () => {
247249

248250
await resetDirs(appOutDir, electronOutDir)
249251

250-
await build({
252+
const builder = await createBuilder({
251253
configFile: false,
252254
root,
253255
build: {
@@ -273,6 +275,7 @@ describe('src/multi-env', () => {
273275
]),
274276
logLevel: 'silent',
275277
})
278+
await builder.buildApp()
276279

277280
const mainCode = fs
278281
.readFileSync(path.join(electronOutDir, 'env-status.js'), 'utf-8')
@@ -289,7 +292,7 @@ describe('src/multi-env', () => {
289292

290293
await resetDirs(appOutDir, electronOutDir)
291294

292-
await build({
295+
const builder = await createBuilder({
293296
configFile: false,
294297
root,
295298
build: {
@@ -319,6 +322,7 @@ describe('src/multi-env', () => {
319322
]),
320323
logLevel: 'silent',
321324
})
325+
await builder.buildApp()
322326

323327
const mainCode = fs
324328
.readFileSync(path.join(electronOutDir, 'env-main.js'), 'utf-8')
@@ -336,7 +340,7 @@ describe('src/multi-env', () => {
336340

337341
await resetDirs(appOutDir, mainOutDir, preloadOutDir)
338342

339-
await build({
343+
const builder = await createBuilder({
340344
configFile: false,
341345
root,
342346
build: {
@@ -377,6 +381,7 @@ describe('src/multi-env', () => {
377381
]),
378382
logLevel: 'silent',
379383
})
384+
await builder.buildApp()
380385

381386
const mainCode = fs
382387
.readFileSync(path.join(mainOutDir, 'env-main.js'), 'utf-8')
@@ -416,7 +421,7 @@ describe('src/multi-env', () => {
416421

417422
await resetDirs(appOutDir, mainOutDir, preloadOutDir)
418423

419-
await build({
424+
const builder = await createBuilder({
420425
configFile: false,
421426
root,
422427
build: {
@@ -450,6 +455,7 @@ describe('src/multi-env', () => {
450455
]),
451456
logLevel: 'silent',
452457
})
458+
await builder.buildApp()
453459

454460
const mainCode = fs
455461
.readFileSync(path.join(mainOutDir, 'env-main.js'), 'utf-8')
@@ -476,7 +482,7 @@ describe('src/multi-env', () => {
476482

477483
await resetDirs(appOutDir, mainOutDir, preloadOutDir, logsDir)
478484

479-
await build({
485+
const builder = await createBuilder({
480486
configFile: false,
481487
root,
482488
build: {
@@ -510,6 +516,7 @@ describe('src/multi-env', () => {
510516
]),
511517
logLevel: 'silent',
512518
})
519+
await builder.buildApp()
513520

514521
const mainCode = fs
515522
.readFileSync(path.join(mainOutDir, 'env-main.js'), 'utf-8')
@@ -555,7 +562,7 @@ describe('src/multi-env', () => {
555562

556563
await resetDirs(appOutDir, electronOutDir)
557564

558-
await build({
565+
const builder = await createBuilder({
559566
configFile: false,
560567
root,
561568
build: {
@@ -609,6 +616,7 @@ describe('src/multi-env', () => {
609616
]),
610617
logLevel: 'silent',
611618
})
619+
await builder.buildApp()
612620

613621
const mainCode = fs
614622
.readFileSync(path.join(electronOutDir, 'env-main.js'), 'utf-8')

0 commit comments

Comments
 (0)