Skip to content

Commit 39b5d6f

Browse files
committed
feat: hmr event api
1 parent c041475 commit 39b5d6f

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

packages/commandkit/src/cli/development.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { watch } from 'chokidar';
77
import { debounce } from '../utils/utilities';
88
import colors from '../utils/colors';
99
import { ChildProcess } from 'node:child_process';
10+
import { setTimeout as sleep } from 'node:timers/promises';
1011

1112
async function buildAndStart(configPath: string, skipStart = false) {
1213
const config = await loadConfigFile(configPath);

packages/commandkit/src/plugins/RuntimePlugin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PluginCommon, PluginOptions } from './PluginCommon';
33
import type { CommandKitPluginRuntime } from './runtime/CommandKitPluginRuntime';
44
import { PreparedAppCommandExecution } from '../app';
55
import { CommandKitEnvironment } from '../context/environment';
6+
import { CommandKitHMREvent } from '../utils/dev-hooks';
67

78
export abstract class RuntimePlugin<
89
T extends PluginOptions = PluginOptions,
@@ -106,6 +107,16 @@ export abstract class RuntimePlugin<
106107
public async onCommandsRouterInit(
107108
ctx: CommandKitPluginRuntime,
108109
): Promise<void> {}
110+
111+
/**
112+
* Called when HMR event is received
113+
* @param ctx The context
114+
* @param event The event
115+
*/
116+
public async performHMR(
117+
ctx: CommandKitPluginRuntime,
118+
event: CommandKitHMREvent,
119+
): Promise<void> {}
109120
}
110121

111122
export function isRuntimePlugin(plugin: unknown): plugin is RuntimePlugin {

packages/commandkit/src/utils/dev-hooks.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ import { COMMANDKIT_IS_DEV } from './constants';
44

55
interface IpcMessageCommand {
66
event: string;
7-
path?: string;
7+
path: string;
8+
}
9+
10+
export interface CommandKitHMREvent {
11+
event: string;
12+
path: string;
13+
accept: () => void;
14+
preventDefault: () => void;
815
}
916

1017
export function registerDevHooks(commandkit: CommandKit) {
1118
if (!COMMANDKIT_IS_DEV) return;
1219

13-
process.on('message', (message) => {
20+
process.on('message', async (message) => {
1421
if (typeof message !== 'object' || message === null) return;
1522

1623
const { event, path } = message as IpcMessageCommand;
@@ -20,6 +27,29 @@ export function registerDevHooks(commandkit: CommandKit) {
2027
Logger.info(`Received HMR event: ${event}${path ? ` for ${path}` : ''}`);
2128
}
2229

30+
let accepted = false,
31+
prevented = false;
32+
33+
const hmrEvent: CommandKitHMREvent = {
34+
accept() {
35+
accepted = true;
36+
},
37+
preventDefault() {
38+
prevented = false;
39+
},
40+
path,
41+
event,
42+
};
43+
44+
await commandkit.plugins.execute(async (ctx, plugin) => {
45+
// some plugin accepted the event
46+
if (accepted) return;
47+
await plugin.performHMR?.(ctx, hmrEvent);
48+
});
49+
50+
// plugin took care of it
51+
if (prevented) return;
52+
2353
switch (event) {
2454
case 'reload-commands':
2555
commandkit.commandHandler.reloadCommands();

0 commit comments

Comments
 (0)