Migrate to ECMAScript Modules

Migrate the usage of CommonJS specific code and imports to ESM.  Roughly
summarised this:
- Changes all require() calls to import statement or expressions.
- Changes usage of module.exports to export statements.
- Replaces require.resolve with import.meta.resolve.
- Replaces conditional imports with conditional subpath imports.
- Replaces module === require.main with import.meta.main.
- Adds the full filename with extention to all relative imports.
- Changes all usage of __dirname and __filename to their import.meta
  equivalents.
- Sets "type": "module" in package.json files.
- Removes usage of "use strict".
- Adds .ts an an alias of .js for the webpack build.

Due to differences in how require and import resolves files the plugin
entrypoints will have to include the filename and extension after this
migration in order to work.
This commit is contained in:
Hornwitser committed 2026-09-11 22:43:57 +02:00
1 parent b69111dda3
commit 5dcaca4d0b
261 files changed
+1115 -1330

No files matched your search

@@ -0,0 +1,2 @@
const NativeWebSocket = WebSocket;
export { NativeWebSocket as WebSocket };
-2
View File
@@ -1,2 +0,0 @@
import ws = require("ws");
export = ws;
-3
View File
@@ -1,3 +0,0 @@
"use strict";
module.exports = process.env.APP_ENV === "browser" ? window.WebSocket : require("ws");
+1
View File
@@ -0,0 +1 @@
export { WebSocket } from "ws";
+13 -10
View File
@@ -2,12 +2,12 @@
import { strict as assert } from "assert";
import events from "events";
import WebSocket from "./WebSocket";
import * as libData from "../data";
import * as libErrors from "../errors";
import { logger } from "../logging";
import ExponentialBackoff from "../ExponentialBackoff";
import type { Request, Event } from "./link";
import { WebSocket } from "#WebSocket";
import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import ExponentialBackoff from "../ExponentialBackoff.js";
import type { Request, Event } from "./link.js";
/**
* Numbered codes describing why a connection was closed
@@ -151,7 +151,7 @@ type WebSocketBaseConnectorEvents = {
"drop": [],
};
export type WebSocketClusterio = WebSocket.WebSocket & {
export type WebSocketClusterio = WebSocket & {
clusterio_ignore_dump?: boolean;
};
@@ -275,7 +275,9 @@ export abstract class WebSocketBaseConnector<
if (process.env.APP_ENV === "browser") {
this._socket!.close(ConnectionClosed.Timeout, "Heartbeat timeout");
} else {
this._socket!.terminate();
// This is only available in node, but the typechecking doesn't
// detect that this code is unreachable in the browser build.
(this._socket! as any).terminate();
}
} else {
@@ -635,7 +637,7 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
}
};
this._socket!.onerror = (event) => {
this._socket!.onerror = ((event: { error?: any, message?: string, }) => {
// It's assumed that close is always called by ws
let code = !event.error ? "" : `, code: ${event.error.code}`;
let message = `Connector | Socket error: ${event.message || "unknown error"}${code}`;
@@ -657,7 +659,8 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
this._closing = true;
}
}
};
// The event paramater have conflicting types in the node and browser versions, ignore it.
}) as any;
this._socket!.onopen = () => {
logger.verbose("Connector | Open");
+2 -2
View File
@@ -3,5 +3,5 @@
* @module lib/link
* @author Hornwitser
*/
export * from "./link";
export * from "./connectors";
export * from "./link.js";
export * from "./connectors.js";
+8 -8
View File
@@ -1,15 +1,15 @@
// Implementation of Link class
import * as libData from "../data";
import * as libErrors from "../errors";
import { logger } from "../logging";
import * as libSchema from "../schema";
import { dataClasses } from "./messages";
import { BaseConnector, WebSocketBaseConnector } from "./connectors";
import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import * as libSchema from "../schema.js";
import { dataClasses } from "./messages.js";
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
import { strict as assert } from "assert";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin.js";
import type {
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
} from "../data";
} from "../data/index.js";
export interface Request<Req, Res> {
constructor: Partial<JSONDeserialisable<Req & Request<Req, Res>>> & {
+10 -10
View File
@@ -1,13 +1,13 @@
import * as core from "../data/messages_core";
import * as controller from "../data/messages_controller";
import * as host from "../data/messages_host";
import * as instance from "../data/messages_instance";
import * as mod from "../data/messages_mod";
import * as user from "../data/messages_user";
import * as role from "../data/messages_role";
import * as plugin from "../data/messages_plugin";
import * as subscriptions from "../subscriptions";
import type { RequestClass, EventClass } from "./link";
import * as core from "../data/messages_core.js";
import * as controller from "../data/messages_controller.js";
import * as host from "../data/messages_host.js";
import * as instance from "../data/messages_instance.js";
import * as mod from "../data/messages_mod.js";
import * as user from "../data/messages_user.js";
import * as role from "../data/messages_role.js";
import * as plugin from "../data/messages_plugin.js";
import * as subscriptions from "../subscriptions.js";
import type { RequestClass, EventClass } from "./link.js";
/**
* Event and Request payloads registered with the Link class. Used to decode