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

+3 -8
View File
@@ -1,6 +1,5 @@
"use strict";
const { Command, CommandTree } = require("@clusterio/lib");
const { BaseCtlPlugin } = require("@clusterio/ctl");
import { Command, CommandTree } from "@clusterio/lib";
import { BaseCtlPlugin } from "@clusterio/ctl";
const testPluginCommands = new CommandTree({
name: "test-plugin", description: "Test plugin commands",
@@ -15,12 +14,8 @@ testPluginCommands.add(new Command({
},
}));
class CtlPlugin extends BaseCtlPlugin {
export class CtlPlugin extends BaseCtlPlugin {
async addCommands(rootCommand) {
rootCommand.add(testPluginCommands);
}
}
module.exports = {
CtlPlugin,
};
+3 -8
View File
@@ -1,9 +1,8 @@
"use strict";
const { BaseControllerPlugin } = require("@clusterio/controller");
import { BaseControllerPlugin } from "@clusterio/controller";
const { ControllerEcho, HostEcho } = require("./messages");
import { ControllerEcho, HostEcho } from "./messages.js";
class ControllerPlugin extends BaseControllerPlugin {
export class ControllerPlugin extends BaseControllerPlugin {
async init() {
this.logger.info("test_plugin controller loaded");
this.controller.handle(ControllerEcho, this.handleControllerEcho.bind(this));
@@ -15,7 +14,3 @@ class ControllerPlugin extends BaseControllerPlugin {
return request.text;
}
}
module.exports = {
ControllerPlugin,
};
+3 -8
View File
@@ -1,9 +1,8 @@
"use strict";
const { BaseHostPlugin } = require("@clusterio/host");
import { BaseHostPlugin } from "@clusterio/host";
const { HostEcho, HostEchoReceived } = require("./messages");
import { HostEcho, HostEchoReceived } from "./messages.js";
class HostPlugin extends BaseHostPlugin {
export class HostPlugin extends BaseHostPlugin {
async init() {
this.logger.info("test_plugin host loaded");
this.receivedEchoes = new Set();
@@ -20,7 +19,3 @@ class HostPlugin extends BaseHostPlugin {
return this.receivedEchoes.has(request.text);
}
}
module.exports = {
HostPlugin,
};
+14 -17
View File
@@ -1,24 +1,21 @@
"use strict";
// Plugin exercising the plugin system in the integration tests, see #342.
// Written as plain JavaScript so that it does not need to be built.
const { ControllerEcho, HostEcho, HostEchoReceived } = require("./messages");
import { ControllerEcho, HostEcho, HostEchoReceived } from "./messages.js";
module.exports = {
plugin: {
name: "test_plugin",
title: "Test Plugin",
description: "Plugin used to test the plugin system.",
export const plugin = {
name: "test_plugin",
title: "Test Plugin",
description: "Plugin used to test the plugin system.",
controllerEntrypoint: "controller",
hostEntrypoint: "host",
instanceEntrypoint: "instance",
ctlEntrypoint: "control",
controllerEntrypoint: "controller.js",
hostEntrypoint: "host.js",
instanceEntrypoint: "instance.js",
ctlEntrypoint: "control.js",
messages: [
ControllerEcho,
HostEcho,
HostEchoReceived,
],
},
messages: [
ControllerEcho,
HostEcho,
HostEchoReceived,
],
};
+2 -7
View File
@@ -1,12 +1,7 @@
"use strict";
const { BaseInstancePlugin } = require("@clusterio/host");
import { BaseInstancePlugin } from "@clusterio/host";
class InstancePlugin extends BaseInstancePlugin {
export class InstancePlugin extends BaseInstancePlugin {
async init() {
this.logger.info("test_plugin instance loaded");
}
}
module.exports = {
InstancePlugin,
};
+4 -11
View File
@@ -1,9 +1,8 @@
"use strict";
const { JsonBoolean, JsonString } = require("@clusterio/lib");
import { JsonBoolean, JsonString } from "@clusterio/lib";
// Sent from ctl to the controller, which answers with the text it was given
// and broadcasts a HostEcho to the hosts.
class ControllerEcho {
export class ControllerEcho {
static type = "request";
static src = "control";
static dst = "controller";
@@ -21,7 +20,7 @@ class ControllerEcho {
}
// Broadcast from the controller to the hosts by broadcastEventToHosts.
class HostEcho {
export class HostEcho {
static type = "event";
static src = "controller";
static dst = "host";
@@ -39,7 +38,7 @@ class HostEcho {
// Sent from ctl to a host, which answers with whether it received the given
// text via a HostEcho.
class HostEchoReceived {
export class HostEchoReceived {
static type = "request";
static src = "control";
static dst = "host";
@@ -55,9 +54,3 @@ class HostEchoReceived {
toJSON() { return this.text; }
static fromJSON(json) { return new this(json); }
}
module.exports = {
ControllerEcho,
HostEcho,
HostEchoReceived,
};
+1
View File
@@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "Plugin used to test the plugin system, see #342",
"private": true,
"type": "module",
"main": "index.js",
"keywords": [
"clusterio-plugin"