Skip to content

Commit 510038e

Browse files
refactor(socket): load Socket.IO client via ESM
1 parent de86c9a commit 510038e

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<div class="region bottom right"><div class="container"></div></div>
4242
</div>
4343
<div class="region fullscreen above"><div class="container"></div></div>
44-
<script type="text/javascript" src="socket.io/socket.io.js"></script>
4544
<script type="text/javascript" src="node_modules/nunjucks/browser/nunjucks.min.js"></script>
4645
<script type="text/javascript" src="js/vendor.js"></script>
4746
<script type="text/javascript" src="defaultmodules/defaultmodules.js"></script>

js/socketclient.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
// eslint-disable-next-line import-x/no-unresolved -- Socket.IO serves this module at runtime.
2+
const { io } = await import(/* @vite-ignore */ "/socket.io/socket.io.esm.min.js");
3+
14
export const MMSocket = function (moduleName) {
25
if (typeof moduleName !== "string") {
36
throw new Error("Please set the module name for the MMSocket.");
47
}
58

6-
const ioClient = globalThis.io;
7-
if (typeof ioClient !== "function") {
8-
throw new Error("socket.io client is not available.");
9-
}
10-
119
this.moduleName = moduleName;
1210

1311
// Private Methods
1412
const base = globalThis.config?.basePath ?? "/";
15-
this.socket = ioClient(`/${this.moduleName}`, {
13+
this.socket = io(`/${this.moduleName}`, {
1614
path: `${base}socket.io`,
1715
pingInterval: 120000, // send pings every 2 mins
1816
pingTimeout: 120000 // wait up to 2 mins for a pong
@@ -45,5 +43,8 @@ export const MMSocket = function (moduleName) {
4543
};
4644
};
4745

46+
// Legacy global bridge for third-party modules that reference io directly.
47+
globalThis.io = io;
48+
4849
// Legacy global bridge for third-party modules that reference MMSocket directly.
4950
if (!globalThis.MMSocket) globalThis.MMSocket = MMSocket;

tests/utils/vitest-setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
const Module = require("node:module");
77
const path = require("node:path");
88

9+
vi.doMock(path.resolve(__dirname, "../..", "js", "socketclient.js"), () => ({
10+
MMSocket () {}
11+
}));
12+
913
// Set test mode flag for application code to detect test environment
1014
process.env.mmTestMode = "true";
1115

0 commit comments

Comments
 (0)