From 990b267ba1ffdc5950b0bd06b6156c6d4ca3e049 Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Tue, 4 Aug 2026 08:32:33 +0200 Subject: [PATCH] refactor(socket): use direct ESM import in main --- js/main.js | 20 +++++++++----------- js/socketclient.js | 9 ++++++++- tests/utils/vitest-setup.js | 1 + 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/js/main.js b/js/main.js index a9ab5b8382..01419a2596 100644 --- a/js/main.js +++ b/js/main.js @@ -3,6 +3,7 @@ // Ensure Module global bridge is initialized before main bootstrap logic runs. import "./module.js"; import { loadModules } from "./loader.js"; +import { io } from "./socketclient.js"; import { Translator } from "./translator.js"; let modules = []; @@ -593,17 +594,14 @@ export const MM = { createDomObjects(); // Setup global socket listener for RELOAD event (watch mode) - const ioClient = globalThis.io; - if (typeof ioClient !== "undefined") { - const socket = ioClient("/", { - path: `${config.basePath || "/"}socket.io` - }); - - socket.on("RELOAD", () => { - Log.warn("Reload notification received from server"); - window.location.reload(true); - }); - } + const socket = io("/", { + path: `${config.basePath || "/"}socket.io` + }); + + socket.on("RELOAD", () => { + Log.warn("Reload notification received from server"); + window.location.reload(true); + }); if (config.reloadAfterServerRestart) { setInterval(async () => { diff --git a/js/socketclient.js b/js/socketclient.js index 29f5d18c88..8e52b694c0 100644 --- a/js/socketclient.js +++ b/js/socketclient.js @@ -1,5 +1,12 @@ // eslint-disable-next-line import-x/no-unresolved -- Socket.IO serves this module at runtime. -const { io } = await import(/* @vite-ignore */ "/socket.io/socket.io.esm.min.js"); +const socketIo = await import(/* @vite-ignore */ "/socket.io/socket.io.esm.min.js"); +const { io } = socketIo; + +if (typeof io !== "function") { + throw new Error("Socket.IO client did not provide a callable io export."); +} + +export { io }; export const MMSocket = function (moduleName) { if (typeof moduleName !== "string") { diff --git a/tests/utils/vitest-setup.js b/tests/utils/vitest-setup.js index e5d47f8abe..5b8121b9fe 100644 --- a/tests/utils/vitest-setup.js +++ b/tests/utils/vitest-setup.js @@ -7,6 +7,7 @@ const Module = require("node:module"); const path = require("node:path"); vi.doMock(path.resolve(__dirname, "../..", "js", "socketclient.js"), () => ({ + io () {}, MMSocket () {} }));