Skip to content

Commit

Permalink
feat(plugin-vite): hot-restart 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Apr 5, 2023
1 parent 1cb2f99 commit e243a4b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/plugin/vite/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export interface VitePluginBuildConfig {
* Vite config file path.
*/
config?: string;
/**
* By default, when any entry in `build` is rebuilt it will restart the Electron App.
* If you want to customize this behavior, you can pass a function and control it with the `args.restart` provided by the function.
*/
restart?: false | ((args: { restart: () => void }) => void | Promise<void>);
}

export interface VitePluginRendererConfig {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin/vite/src/ViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import debug from 'debug';
import { ConfigEnv, loadConfigFromFile, mergeConfig, UserConfig } from 'vite';

import { VitePluginConfig } from './Config';
import { externalBuiltins } from './util/plugins';
import { externalBuiltins, hotRestart } from './util/plugins';

const d = debug('electron-forge:plugin:vite:viteconfig');

Expand Down Expand Up @@ -68,7 +68,7 @@ export default class ViteConfigGenerator {
const plugins = [externalBuiltins()];
const configs = this.pluginConfig.build
.filter(({ entry, config }) => entry || config)
.map<Promise<UserConfig>>(async ({ entry, config }) => {
.map<Promise<UserConfig>>(async ({ entry, config, restart }) => {
const defaultConfig: UserConfig = {
// Ensure that each build config loads the .env file correctly.
mode: this.mode,
Expand All @@ -89,7 +89,7 @@ export default class ViteConfigGenerator {
},
clearScreen: false,
define,
plugins,
plugins: [...plugins, hotRestart(restart)],
};
if (config) {
const loadResult = await this.resolveConfig(config);
Expand Down
43 changes: 42 additions & 1 deletion packages/plugin/vite/src/util/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { builtinModules } from 'node:module';

import type { Plugin } from 'vite';
import { VitePluginBuildConfig } from '../Config';

import type { Plugin, ResolvedConfig } from 'vite';

/**
* `electron` and Node.js built-in modules should always be externalize.
Expand Down Expand Up @@ -33,3 +35,42 @@ export function externalBuiltins() {
},
};
}

/**
* Hot restart App during development for DX.
*/
export function hotRestart(options: VitePluginBuildConfig['restart']) {
let config: ResolvedConfig;
const restart = () => {
// https://github.com/electron/forge/blob/v6.0.5/packages/api/core/src/api/start.ts#L204-L211
process.stdin.emit('data', 'rs');
};
// Avoid first start, it's stated by forge.
let isFirstStart = false;

return <Plugin>{
name: '@electron-forge/plugin-vite:hot-restart',
configResolved(_config) {
config = _config;
},
closeBundle() {
if (config.mode === 'production') {
// https://github.com/electron/forge/blob/v6.1.1/packages/plugin/vite/src/ViteConfig.ts#L36-L41
return;
}
if (options === false) {
return;
}
if (!isFirstStart) {
isFirstStart = true;
return;
}
if (typeof options === 'function') {
// Leave it to the user to decide whether to restart.
options({ restart });
} else {
restart();
}
},
};
}
2 changes: 1 addition & 1 deletion packages/plugin/vite/test/ViteConfig_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('ViteConfigGenerator', () => {
clearScreen: false,
define: {},
// shims
plugins: [buildConfig.plugins?.[0]],
plugins: buildConfig.plugins,
} as UserConfig);
});

Expand Down

0 comments on commit e243a4b

Please sign in to comment.