@@ -3,7 +3,16 @@ import * as path from 'path';
3
3
4
4
const variableNameRegex = / ^ [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * $ / ;
5
5
6
- export function generate ( nodeModulesDirs : string [ ] ) {
6
+ export async function generate (
7
+ nodeModulesDirs : string [ ] ,
8
+ loader = async ( mod : string ) => {
9
+ try {
10
+ return require ( mod ) ;
11
+ } catch {
12
+ return await import ( mod ) ;
13
+ }
14
+ }
15
+ ) {
7
16
let indentLevel = 0 ;
8
17
let dts = '' ;
9
18
let defId = 0 ;
@@ -26,7 +35,7 @@ export function generate(nodeModulesDirs: string[]) {
26
35
for ( const subPkg of subPkgs ) {
27
36
if ( subPkg === 'eslint-plugin' ) {
28
37
const pluginName = `${ pkg } /${ subPkg } ` ;
29
- let plugin = require ( pluginName ) ;
38
+ let plugin = await loader ( pluginName ) ;
30
39
if ( 'default' in plugin ) {
31
40
plugin = plugin . default ;
32
41
}
@@ -43,7 +52,7 @@ export function generate(nodeModulesDirs: string[]) {
43
52
}
44
53
}
45
54
else if ( pkg . startsWith ( 'eslint-plugin-' ) ) {
46
- let plugin = require ( pkg ) ;
55
+ let plugin = await loader ( pkg ) ;
47
56
if ( 'default' in plugin ) {
48
57
plugin = plugin . default ;
49
58
}
@@ -61,7 +70,7 @@ export function generate(nodeModulesDirs: string[]) {
61
70
for ( const ruleFile of ruleFiles ) {
62
71
if ( ruleFile . endsWith ( '.js' ) ) {
63
72
const ruleName = ruleFile . replace ( '.js' , '' ) ;
64
- const rule = require ( path . join ( rulesDir , ruleFile ) ) ;
73
+ const rule = await loader ( path . join ( rulesDir , ruleFile ) ) ;
65
74
addRule ( undefined , ruleName , rule ) ;
66
75
}
67
76
}
0 commit comments