Skip to content

Commit

Permalink
fix: fallback to require.resolve for Yarn P'n'P
Browse files Browse the repository at this point in the history
close #3936
  • Loading branch information
JounQin committed Feb 28, 2024
1 parent 9351b8e commit 1cfe15c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions @commitlint/resolve-extends/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import {createRequire} from 'module';
import path from 'path';
import {pathToFileURL, fileURLToPath} from 'url';

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

const require = createRequire(import.meta.url);

const dynamicImport = async <T>(id: string): Promise<T> => {
const imported = await import(
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
Expand Down Expand Up @@ -38,14 +41,14 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
}
}

const parentDir =
parent &&
(fs.statSync(parent).isDirectory() ? parent : path.dirname(parent));

let resolveError: Error | undefined;

const base = pathToFileURL(
parent
? fs.statSync(parent).isDirectory()
? path.join(parent, 'noop.js')
: parent
: import.meta.url
parentDir ? path.join(parentDir, 'noop.js') : import.meta.url
);

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

throw resolveError;
try {
/**
* Yarn P'n'P does not support pure ESM well, this is only a workaround for
* @see https://github.com/conventional-changelog/commitlint/issues/3936
*/
return require.resolve(lookup, {
paths: parentDir ? [parentDir] : undefined,
});
} catch {
throw resolveError;
}
};

/**
Expand Down

0 comments on commit 1cfe15c

Please sign in to comment.