Always use async import()

import() works with CommonJS and ESM modules, so we don't need two
different code paths.
This commit is contained in:
valadaptive
2023-12-17 12:29:54 -05:00
parent 7ae0e05946
commit 0162d54e5b

View File

@ -129,7 +129,7 @@ async function loadFromPackage(app, packageJsonPath, exitHooks) {
*/
async function loadFromFile(app, pluginFilePath, exitHooks) {
try {
const plugin = await getPluginModule(pluginFilePath);
const plugin = await import(pluginFilePath);
console.log(`Initializing plugin from ${pluginFilePath}`);
return await initPlugin(app, plugin, exitHooks);
} catch (error) {
@ -198,21 +198,6 @@ async function initPlugin(app, plugin, exitHooks) {
return true;
}
/**
* Loads a module from a file depending on the module type.
* @param {string} pluginFilePath Path to plugin file
* @returns {Promise<any>} Promise that resolves to plugin module
*/
async function getPluginModule(pluginFilePath) {
if (isCommonJS(pluginFilePath)) {
return require(pluginFilePath);
}
if (isESModule(pluginFilePath)) {
return await import(pluginFilePath);
}
throw new Error(`Unsupported module type in ${pluginFilePath}`);
}
module.exports = {
loadPlugins,
};