Skip to content

Commit d194eae

Browse files
authored
fix(package-json): refine ESM logic to return default export only when it is the only available export (#658)
1 parent cc4d7d6 commit d194eae

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/package-json.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,26 @@ export async function loadPlugin(path: string): Promise<Plugin> {
4040
* From Node 22 requiring on ESM module returns the module object
4141
* @see https://github.com/bmish/eslint-doc-generator/issues/615
4242
*/
43-
type cjsOrEsmPlugin = Plugin | { __esModule: boolean; default: Plugin };
43+
type cjsOrEsmPlugin =
44+
| Plugin
45+
| {
46+
__esModule: boolean;
47+
default: Plugin;
48+
/* some plugins might have additional exports besides `default` */
49+
[key: string]: unknown;
50+
};
4451
// eslint-disable-next-line import/no-dynamic-require
4552
const _plugin = require(pluginRoot) as cjsOrEsmPlugin;
4653

4754
/* istanbul ignore next */
48-
if ('__esModule' in _plugin && _plugin.__esModule && _plugin.default) {
55+
if (
56+
'__esModule' in _plugin &&
57+
_plugin.__esModule &&
58+
// Ensure that we return only the default key when only a default export is present
59+
// @see https://github.com/bmish/eslint-doc-generator/issues/656#issuecomment-2726745618
60+
Object.keys(_plugin).length === 2 &&
61+
['__esModule', 'default'].every((it) => Boolean(_plugin[it]))
62+
) {
4963
return _plugin.default;
5064
}
5165
return _plugin as Plugin;

0 commit comments

Comments
 (0)