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

+15 -15
View File
@@ -1,10 +1,10 @@
"use strict";
const assert = require("assert").strict;
const fs = require("node:fs/promises");
const path = require("path");
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import { pathToFileURL } from "node:url";
const lib = require("@clusterio/lib");
const { BaseControllerPlugin } = require("@clusterio/controller");
import * as lib from "@clusterio/lib";
import { BaseControllerPlugin } from "@clusterio/controller";
describe("lib/plugin_loader", function() {
@@ -19,7 +19,7 @@ describe("lib/plugin_loader", function() {
await fs.mkdir(pluginPath, { recursive: true });
await fs.writeFile(
path.join(pluginPath, "index.js"),
`module.exports.plugin = { name: "${infoName}" };`
`export const plugin = { name: "${infoName}" };`
);
await fs.writeFile(
path.join(pluginPath, "package.json"),
@@ -56,7 +56,7 @@ describe("lib/plugin_loader", function() {
it("should reject on broken plugin", async function() {
let brokenMessage;
try {
require(path.resolve(brokenPlugin));
await import(pathToFileURL(path.resolve(path.join(brokenPlugin, "index.js"))));
} catch (err) {
brokenMessage = err.message;
}
@@ -84,7 +84,7 @@ describe("lib/plugin_loader", function() {
await writeEntrypoint(missingClass, "");
await writeEntrypoint(
wrongParentClass, "class ControllerPlugin {}\n module.exports = { ControllerPlugin };\n"
wrongParentClass, "export class ControllerPlugin {};\n"
);
});
it("should throw if class is missing from entrypoint", async function() {
@@ -92,13 +92,13 @@ describe("lib/plugin_loader", function() {
await assert.rejects(
lib.loadPluginClass(
"test",
path.posix.join(requirePath, "controller"),
path.posix.join(requirePath, "controller.js"),
"ControllerPlugin",
BaseControllerPlugin,
),
{
message:
`PluginError: Expected ${path.posix.join(requirePath, "controller")} ` +
`PluginError: Expected ${path.posix.join(requirePath, "controller.js")} ` +
"to export a class named ControllerPlugin",
}
);
@@ -108,14 +108,14 @@ describe("lib/plugin_loader", function() {
await assert.rejects(
lib.loadPluginClass(
"test",
path.posix.join(requirePath, "controller"),
path.posix.join(requirePath, "controller.js"),
"ControllerPlugin",
BaseControllerPlugin,
),
{
message:
"PluginError: Expected ControllerPlugin exported from " +
`${path.posix.join(requirePath, "controller")} to be a subclass of BaseControllerPlugin`,
`${path.posix.join(requirePath, "controller.js")} to be a subclass of BaseControllerPlugin`,
}
);
});
@@ -152,7 +152,7 @@ describe("lib/plugin_loader", function() {
await fs.mkdir(pluginPath, { recursive: true });
await fs.writeFile(
path.join(pluginPath, "index.js"),
`module.exports.plugin = { name: "${name}" };`
`export const plugin = { name: "${name}" };`
);
await fs.writeFile(
path.join(pluginPath, "package.json"),
@@ -195,7 +195,7 @@ describe("lib/plugin_loader", function() {
await fs.mkdir(path.join(deepPluginPath, "dist"));
await fs.writeFile(
path.join(deepPluginPath, "dist", "index.js"),
'module.exports.plugin = { name: "deep" };'
'export const plugin = { name: "deep" };'
);
// Create an npm module that is not a plugin