1
1
import fs from 'fs' ;
2
+ import { createRequire } from 'module' ;
2
3
import path from 'path' ;
3
4
import { pathToFileURL , fileURLToPath } from 'url' ;
4
5
@@ -8,6 +9,8 @@ import mergeWith from 'lodash.mergewith';
8
9
import { validateConfig } from '@commitlint/config-validator' ;
9
10
import type { ParserPreset , UserConfig } from '@commitlint/types' ;
10
11
12
+ const require = createRequire ( import . meta. url ) ;
13
+
11
14
const dynamicImport = async < T > ( id : string ) : Promise < T > => {
12
15
const imported = await import (
13
16
path . isAbsolute ( id ) ? pathToFileURL ( id ) . toString ( ) : id
@@ -38,14 +41,14 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
38
41
}
39
42
}
40
43
44
+ const parentDir =
45
+ parent &&
46
+ ( fs . statSync ( parent ) . isDirectory ( ) ? parent : path . dirname ( parent ) ) ;
47
+
41
48
let resolveError : Error | undefined ;
42
49
43
50
const base = pathToFileURL (
44
- parent
45
- ? fs . statSync ( parent ) . isDirectory ( )
46
- ? path . join ( parent , 'noop.js' )
47
- : parent
48
- : import . meta. url
51
+ parentDir ? path . join ( parentDir , 'noop.js' ) : import . meta. url
49
52
) ;
50
53
51
54
for ( const suffix of specifierSuffixes ) {
@@ -58,7 +61,17 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
58
61
}
59
62
}
60
63
61
- throw resolveError ;
64
+ try {
65
+ /**
66
+ * Yarn P'n'P does not support pure ESM well, this is only a workaround for
67
+ * @see https://github.com/conventional-changelog/commitlint/issues/3936
68
+ */
69
+ return require . resolve ( lookup , {
70
+ paths : parentDir ? [ parentDir ] : undefined ,
71
+ } ) ;
72
+ } catch {
73
+ throw resolveError ;
74
+ }
62
75
} ;
63
76
64
77
/**
0 commit comments