Skip to content

Commit 1631a3e

Browse files
authored
fix(plugin-vite): clearer bundle error state (#3987)
* fix(plugin-vite): clearer bundle error state * only log rollup messages depending on vite `logLevel` in userConfig
1 parent a064bb9 commit 1631a3e

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

packages/plugin/vite/src/VitePlugin.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import fs from 'fs-extra';
1010
import { Listr, PRESET_TIMER } from 'listr2';
1111
import { default as vite } from 'vite';
1212

13-
import { onBuildDone } from './util/plugins';
1413
import ViteConfigGenerator from './ViteConfig';
1514

1615
import type { VitePluginConfig } from './Config';
@@ -212,6 +211,10 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}.`);
212211
// Main process, Preload scripts and Worker process, etc.
213212
build = async (task?: ForgeListrTask<null>): Promise<Listr | void> => {
214213
const configs = await this.configGenerator.getBuildConfigs();
214+
/**
215+
* Checks if the result of the Vite build is a Rollup watcher.
216+
* This should happen iff we're running `electron-forge start`.
217+
*/
215218
const isRollupWatcher = (
216219
x:
217220
| vite.Rollup.RollupWatcher
@@ -265,23 +268,50 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}.`);
265268
.build({
266269
// Avoid recursive builds caused by users configuring @electron-forge/plugin-vite in Vite config file.
267270
configFile: false,
268-
logLevel: 'silent', // We suppress Vite output and instead log lines using RollupWatcher events
271+
// We suppress Vite output and instead log lines using RollupWatcher events
272+
logLevel: 'silent',
269273
...userConfig,
270274
plugins: [
271-
onBuildDone(resolve),
275+
// This plugin controls the output of the first-time Vite build that happens.
276+
// `buildEnd` and `closeBundle` are Rollup output generation hooks.
277+
// See https://rollupjs.org/plugin-development/#output-generation-hooks
278+
{
279+
name: '@electron-forge/plugin-vite:build-done',
280+
buildEnd(err) {
281+
if (err instanceof Error) {
282+
d(
283+
'buildEnd rollup hook called with error so build failed',
284+
);
285+
reject(err);
286+
}
287+
},
288+
closeBundle() {
289+
d(
290+
'no error in buildEnd and reached closeBundle so build succeeded',
291+
);
292+
resolve();
293+
},
294+
},
272295
...(userConfig.plugins ?? []),
273296
],
274297
clearScreen: false,
275298
})
276299
.then((result) => {
300+
// When running `start` and enabling watch mode in Vite, the Rollup watcher
301+
// emits events for subsequent builds.
277302
if (isRollupWatcher(result)) {
278303
result.on('event', (event) => {
279-
if (event.code === 'ERROR') {
304+
if (
305+
event.code === 'ERROR' &&
306+
userConfig.logLevel !== 'silent'
307+
) {
280308
console.error(
281-
`\n${this.timeFormatter.format(new Date())} ${event.error.message}`,
309+
`\n${chalk.dim(this.timeFormatter.format(new Date()))} ${event.error.message}`,
282310
);
283-
reject(event.error);
284-
} else if (event.code === 'BUNDLE_END') {
311+
} else if (
312+
event.code === 'BUNDLE_END' &&
313+
(!userConfig.logLevel || userConfig.logLevel === 'info')
314+
) {
285315
console.log(
286316
`${chalk.dim(this.timeFormatter.format(new Date()))} ${chalk.cyan.bold('[@electron-forge/plugin-vite]')} ${chalk.green(
287317
'target built',
@@ -298,14 +328,11 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}.`);
298328
.catch(reject);
299329
});
300330
},
301-
rendererOptions: {
302-
persistentOutput: true,
303-
},
304-
exitOnError: true,
305331
};
306332
}),
307333
{
308334
concurrent: this.config.concurrent ?? true,
335+
exitOnError: this.isProd,
309336
},
310337
);
311338
};

packages/plugin/vite/src/util/plugins.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)