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.
This commit is contained in:
2026-08-09 23:13:04 +08:00
parent 5808e15c45
commit 4727a00e26
+13 -1
View File
@@ -16,7 +16,7 @@
* hot-reloads the app via `addWatchFile`. * hot-reloads the app via `addWatchFile`.
*/ */
import * as path from 'node:path'; import * as path from 'node:path';
import type { Plugin } from 'vite'; import type { ModuleNode, Plugin } from 'vite';
import { collectPackages, loadDefs } from './collect.js'; import { collectPackages, loadDefs } from './collect.js';
import type { Package, SerializedPackage } from './types.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 === PACKAGES) return VIRTUAL_PREFIX + PACKAGES;
if (id.startsWith(PACKAGE)) return VIRTUAL_PREFIX + id; 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) { load(id) {
if (!id.startsWith(VIRTUAL_PREFIX)) return; if (!id.startsWith(VIRTUAL_PREFIX)) return;
const virtual = id.slice(VIRTUAL_PREFIX.length); const virtual = id.slice(VIRTUAL_PREFIX.length);