Plugins: support .cjs module extension

ffa8716d070689fd84c3a3bfd07e6fed36e261b1

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +8 -12Showing whitespace changes
src/plugin-loader.js+8 -12
@@ -17,7 +17,7 @@ const loadedPlugins = new Map();
17 * @param {string} file Path to file17 * @param {string} file Path to file
18 * @returns {boolean} True if file is a CommonJS module18 * @returns {boolean} True if file is a CommonJS module
19 */19 */
20const isCommonJS = (file) => path.extname(file) === '.js';20const isCommonJS = (file) => path.extname(file) === '.js' || path.extname(file) === '.cjs';
2121
22/**22/**
23 * Determine if a file is an ECMAScript module.23 * Determine if a file is an ECMAScript module.
@@ -90,22 +90,18 @@ async function loadFromDirectory(app, pluginDirectoryPath, exitHooks) {
90 }90 }
91 }91 }
9292
93 // Plugin is a CommonJS module.93 // Plugin is a module file.
94 const cjsFilePath = path.join(pluginDirectoryPath, 'index.js');94 const fileTypes = ['index.js', 'index.cjs', 'index.mjs'];
95 if (fs.existsSync(cjsFilePath)) {
96 if (await loadFromFile(app, cjsFilePath, exitHooks)) {
97 return;
98 }
99 }
10095
101 // Plugin is an ECMAScript module.96 for (const fileType of fileTypes) {
102 const esmFilePath = path.join(pluginDirectoryPath, 'index.mjs');97 const filePath = path.join(pluginDirectoryPath, fileType);
103 if (fs.existsSync(esmFilePath)) {98 if (fs.existsSync(filePath)) {
104 if (await loadFromFile(app, esmFilePath, exitHooks)) {99 if (await loadFromFile(app, filePath, exitHooks)) {
105 return;100 return;
106 }101 }
107 }102 }
108 }103 }
104}
109105
110/**106/**
111 * Loads and initializes a plugin from an npm package.107 * Loads and initializes a plugin from an npm package.