From 1cfe15cacc46e3234f8984273ccf10b225aa967b Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 28 Feb 2024 11:48:09 +0800 Subject: [PATCH 1/2] fix: fallback to `require.resolve` for Yarn P'n'P close #3936 --- @commitlint/resolve-extends/src/index.ts | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/@commitlint/resolve-extends/src/index.ts b/@commitlint/resolve-extends/src/index.ts index 744f357cff..c90bf96211 100644 --- a/@commitlint/resolve-extends/src/index.ts +++ b/@commitlint/resolve-extends/src/index.ts @@ -1,4 +1,5 @@ import fs from 'fs'; +import {createRequire} from 'module'; import path from 'path'; import {pathToFileURL, fileURLToPath} from 'url'; @@ -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 (id: string): Promise => { const imported = await import( path.isAbsolute(id) ? pathToFileURL(id).toString() : id @@ -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) { @@ -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; + } }; /** From eff775e99ccc1ca62a0ef256493471dd69f1f5b2 Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 28 Feb 2024 12:08:08 +0800 Subject: [PATCH 2/2] refactor: use `resolve-from` package --- @commitlint/resolve-extends/package.json | 3 ++- @commitlint/resolve-extends/src/index.ts | 18 +++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/@commitlint/resolve-extends/package.json b/@commitlint/resolve-extends/package.json index 740268faf9..69994ed0fd 100644 --- a/@commitlint/resolve-extends/package.json +++ b/@commitlint/resolve-extends/package.json @@ -44,7 +44,8 @@ "@commitlint/types": "^19.0.0", "global-directory": "^4.0.1", "import-meta-resolve": "^4.0.0", - "lodash.mergewith": "^4.6.2" + "lodash.mergewith": "^4.6.2", + "resolve-from": "^5.0.0" }, "gitHead": "d829bf6260304ca8d6811f329fcdd1b6c50e9749" } diff --git a/@commitlint/resolve-extends/src/index.ts b/@commitlint/resolve-extends/src/index.ts index c90bf96211..f699e5840f 100644 --- a/@commitlint/resolve-extends/src/index.ts +++ b/@commitlint/resolve-extends/src/index.ts @@ -1,16 +1,14 @@ import fs from 'fs'; -import {createRequire} from 'module'; import path from 'path'; import {pathToFileURL, fileURLToPath} from 'url'; import globalDirectory from 'global-directory'; import {moduleResolve} from 'import-meta-resolve'; import mergeWith from 'lodash.mergewith'; +import resolveFrom_ from 'resolve-from'; import {validateConfig} from '@commitlint/config-validator'; import type {ParserPreset, UserConfig} from '@commitlint/types'; -const require = createRequire(import.meta.url); - const dynamicImport = async (id: string): Promise => { const imported = await import( path.isAbsolute(id) ? pathToFileURL(id).toString() : id @@ -41,14 +39,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( - parentDir ? path.join(parentDir, 'noop.js') : import.meta.url + parent + ? fs.statSync(parent).isDirectory() + ? path.join(parent, 'noop.js') + : parent + : import.meta.url ); for (const suffix of specifierSuffixes) { @@ -66,9 +64,7 @@ export const resolveFrom = (lookup: string, parent?: string): string => { * 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, - }); + return resolveFrom_(path.dirname(fileURLToPath(base)), lookup); } catch { throw resolveError; }