mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
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:
1 parent
b69111dda3
commit
5dcaca4d0b
261 files changed
+1115
-1330
No files matched your search
@@ -5,9 +5,10 @@
|
||||
*/
|
||||
import path from "path";
|
||||
import fs from "node:fs/promises";
|
||||
import * as libErrors from "./errors";
|
||||
import * as libPlugin from "./plugin";
|
||||
import { logger } from "./logging";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import * as libErrors from "./errors.js";
|
||||
import * as libPlugin from "./plugin.js";
|
||||
import { logger } from "./logging.js";
|
||||
|
||||
|
||||
/**
|
||||
@@ -32,7 +33,9 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
try {
|
||||
const absolute = path.isAbsolute(pluginPath);
|
||||
const packageImport = path.posix.join(pluginPath, "package.json");
|
||||
packagePath = require.resolve(packageImport);
|
||||
packagePath = fileURLToPath(import.meta.resolve(
|
||||
absolute ? pathToFileURL(packageImport).href : packageImport
|
||||
));
|
||||
await fs.access(packagePath, fs.constants.F_OK);
|
||||
} catch (err) {
|
||||
let errMsg = `Plugin path ${pluginPath} does not exist`;
|
||||
@@ -46,8 +49,10 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
}
|
||||
|
||||
try {
|
||||
pluginInfo = require(pluginPath).plugin;
|
||||
pluginPackage = require(packagePath);
|
||||
pluginPackage = (await import(pathToFileURL(packagePath).href, { with: { type: "json" }})).default;
|
||||
pluginInfo = (await import(
|
||||
pathToFileURL(path.posix.join(pluginPath, pluginPackage.main ?? "index.js")).href
|
||||
)).plugin;
|
||||
} catch (err: any) {
|
||||
if (err.code === "InstallationError") {
|
||||
throw err;
|
||||
@@ -89,7 +94,7 @@ export async function loadPluginClass<Class extends { new (...args: any): any }>
|
||||
className: string,
|
||||
pluginClass: Class,
|
||||
): Promise<Class> {
|
||||
let entrypoint = require(requirePath);
|
||||
let entrypoint = await import(pathToFileURL(requirePath).href);
|
||||
if (!entrypoint[className]) {
|
||||
throw new libErrors.PluginError(pluginName,
|
||||
new Error(`Expected ${requirePath} to export a class named ${className}`)
|
||||
|
||||
Reference in new issue
Block a user