Skip to content

Commit 219bc37

Browse files
committed
fix: fallback to for Yarn P'n'P
close conventional-changelog#3936
1 parent 9351b8e commit 219bc37

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

@commitlint/resolve-extends/src/index.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import {createRequire} from 'module';
23
import path from 'path';
34
import {pathToFileURL, fileURLToPath} from 'url';
45

@@ -8,6 +9,8 @@ import mergeWith from 'lodash.mergewith';
89
import {validateConfig} from '@commitlint/config-validator';
910
import type {ParserPreset, UserConfig} from '@commitlint/types';
1011

12+
const require = createRequire(import.meta.url);
13+
1114
const dynamicImport = async <T>(id: string): Promise<T> => {
1215
const imported = await import(
1316
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
@@ -38,14 +41,14 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
3841
}
3942
}
4043

44+
const parentDir =
45+
parent &&
46+
(fs.statSync(parent).isDirectory() ? parent : path.dirname(parent));
47+
4148
let resolveError: Error | undefined;
4249

4350
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
4952
);
5053

5154
for (const suffix of specifierSuffixes) {
@@ -58,7 +61,17 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
5861
}
5962
}
6063

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+
}
6275
};
6376

6477
/**

0 commit comments

Comments
 (0)