From 4727a00e26c4bba6686862f3c5dda6c4d618be3e Mon Sep 17 00:00:00 2001 From: hypercross Date: Sun, 9 Aug 2026 23:13:04 +0800 Subject: [PATCH] fix(bgm): hot-reload game definition edits Invalidate the virtual bgm module on changes under the games root so editing a definition reloads the page instead of requiring a manual refresh. --- packages/bgm/src/vite.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/bgm/src/vite.ts b/packages/bgm/src/vite.ts index ea9b4f0..3314da1 100644 --- a/packages/bgm/src/vite.ts +++ b/packages/bgm/src/vite.ts @@ -16,7 +16,7 @@ * hot-reloads the app via `addWatchFile`. */ import * as path from 'node:path'; -import type { Plugin } from 'vite'; +import type { ModuleNode, Plugin } from 'vite'; import { collectPackages, loadDefs } from './collect.js'; import type { Package, SerializedPackage } from './types.js'; @@ -52,6 +52,18 @@ export function bgm(options: BgmOptions): Plugin { if (id === PACKAGES) return VIRTUAL_PREFIX + PACKAGES; if (id.startsWith(PACKAGE)) return VIRTUAL_PREFIX + id; }, + handleHotUpdate(ctx) { + // Re-collect and invalidate the virtual modules when a game definition + // changes, so edits hot-reload instead of requiring a manual refresh. + if (!ctx.file.startsWith(root)) return; + const invalidated: ModuleNode[] = []; + const mod = ctx.server.moduleGraph.getModuleById(VIRTUAL_PREFIX + PACKAGES); + if (mod) { + ctx.server.moduleGraph.invalidateModule(mod); + invalidated.push(mod); + } + return invalidated; + }, load(id) { if (!id.startsWith(VIRTUAL_PREFIX)) return; const virtual = id.slice(VIRTUAL_PREFIX.length);