File tree 2 files changed +15
-7
lines changed
2 files changed +15
-7
lines changed Original file line number Diff line number Diff line change @@ -35,8 +35,20 @@ async function loadPackageJson(path: string): Promise<PackageJson> {
35
35
export async function loadPlugin ( path : string ) : Promise < Plugin > {
36
36
const pluginRoot = getPluginRoot ( path ) ;
37
37
try {
38
- // Try require first which should work for CJS plugins.
39
- return require ( pluginRoot ) as Plugin ; // eslint-disable-line import/no-dynamic-require
38
+ /**
39
+ * Try require first which should work for CJS plugins.
40
+ * From Node 22 requiring on ESM module returns the module object
41
+ * @see https://github.com/bmish/eslint-doc-generator/issues/615
42
+ */
43
+ type cjsOrEsmPlugin = Plugin | { __esModule : boolean ; default : Plugin } ;
44
+ // eslint-disable-next-line import/no-dynamic-require
45
+ const _plugin = require ( pluginRoot ) as cjsOrEsmPlugin ;
46
+
47
+ /* istanbul ignore next */
48
+ if ( '__esModule' in _plugin && _plugin . __esModule && _plugin . default ) {
49
+ return _plugin . default ;
50
+ }
51
+ return _plugin as Plugin ;
40
52
} catch ( error ) {
41
53
// Otherwise, for ESM plugins, we'll have to try to resolve the exact plugin entry point and import it.
42
54
const pluginPackageJson = await loadPackageJson ( path ) ;
Original file line number Diff line number Diff line change @@ -62,11 +62,7 @@ describe('generate (cjs)', function () {
62
62
63
63
describe ( 'package.json `main` field points to non-existent file' , function ( ) {
64
64
it ( 'throws an error' , async function ( ) {
65
- const FIXTURE_PATH = join (
66
- 'test' ,
67
- 'fixtures' ,
68
- 'cjs-main-file-does-not-exist' ,
69
- ) ;
65
+ const FIXTURE_PATH = join ( FIXTURE_ROOT , 'cjs-main-file-does-not-exist' ) ;
70
66
await expect ( generate ( FIXTURE_PATH ) ) . rejects . toThrow (
71
67
/ C a n n o t f i n d m o d u l e / u,
72
68
) ;
You can’t perform that action at this time.
0 commit comments