From ffa8716d070689fd84c3a3bfd07e6fed36e261b1 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 11 Oct 2024 23:00:37 +0300 Subject: [PATCH] Plugins: support .cjs module extension --- src/plugin-loader.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/plugin-loader.js b/src/plugin-loader.js index 433d9f411..dd97a80df 100644 --- a/src/plugin-loader.js +++ b/src/plugin-loader.js @@ -17,7 +17,7 @@ const loadedPlugins = new Map(); * @param {string} file Path to file * @returns {boolean} True if file is a CommonJS module */ -const isCommonJS = (file) => path.extname(file) === '.js'; +const isCommonJS = (file) => path.extname(file) === '.js' || path.extname(file) === '.cjs'; /** * Determine if a file is an ECMAScript module. @@ -35,7 +35,7 @@ const isESModule = (file) => path.extname(file) === '.mjs'; */ export async function loadPlugins(app, pluginsPath) { const exitHooks = []; - const emptyFn = () => {}; + const emptyFn = () => { }; // Server plugins are disabled. if (!enableServerPlugins) { @@ -90,19 +90,15 @@ async function loadFromDirectory(app, pluginDirectoryPath, exitHooks) { } } - // Plugin is a CommonJS module. - const cjsFilePath = path.join(pluginDirectoryPath, 'index.js'); - if (fs.existsSync(cjsFilePath)) { - if (await loadFromFile(app, cjsFilePath, exitHooks)) { - return; - } - } + // Plugin is a module file. + const fileTypes = ['index.js', 'index.cjs', 'index.mjs']; - // Plugin is an ECMAScript module. - const esmFilePath = path.join(pluginDirectoryPath, 'index.mjs'); - if (fs.existsSync(esmFilePath)) { - if (await loadFromFile(app, esmFilePath, exitHooks)) { - return; + for (const fileType of fileTypes) { + const filePath = path.join(pluginDirectoryPath, fileType); + if (fs.existsSync(filePath)) { + if (await loadFromFile(app, filePath, exitHooks)) { + return; + } } } }