From 2ec9eadf21d029c40974bf7584a83537c008807f Mon Sep 17 00:00:00 2001 From: pfdgithub <3262762+pfdgithub@users.noreply.github.com> Date: Mon, 13 Nov 2023 20:00:26 +0800 Subject: [PATCH] fix: detect esmodule project --- @commitlint/load/src/utils/load-config.ts | 42 +++++++++++++++++------ 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/@commitlint/load/src/utils/load-config.ts b/@commitlint/load/src/utils/load-config.ts index e08353d8c4..638f902cbe 100644 --- a/@commitlint/load/src/utils/load-config.ts +++ b/@commitlint/load/src/utils/load-config.ts @@ -27,7 +27,7 @@ export async function loadConfig( return tsLoaderInstance(...args); }; - const {searchPlaces, loaders} = getDynamicAwaitConfig(); + const {searchPlaces, loaders} = getDynamicAwaitConfig(cwd); const explorer = cosmiconfig(moduleName, { searchPlaces: [ @@ -85,16 +85,38 @@ export const isDynamicAwaitSupported = () => { // If dynamic await is supported (Node >= v20.8.0), support mjs config. // Otherwise, don't support mjs and use synchronous js/cjs loaders. -export const getDynamicAwaitConfig = (): Partial => - isDynamicAwaitSupported() - ? { - searchPlaces: [`.${moduleName}rc.mjs`, `${moduleName}.config.mjs`], - loaders: {}, - } - : { +export const getDynamicAwaitConfig = (cwd?: string): Partial => { + const dynamic = isDynamicAwaitSupported(); + if (dynamic) { + return { + searchPlaces: [`.${moduleName}rc.mjs`, `${moduleName}.config.mjs`], + loaders: {}, + }; + } + + if (cwd) { + let type = null; + try { + const manifestPath = path.join(cwd, 'package.json'); + type = require(manifestPath).type; + } catch (e) { + // Do nothing + } + if (type === 'module') { + return { searchPlaces: [], loaders: { '.cjs': defaultLoadersSync['.cjs'], - '.js': defaultLoadersSync['.js'], }, - }; + }; + } + } + + return { + searchPlaces: [], + loaders: { + '.cjs': defaultLoadersSync['.cjs'], + '.js': defaultLoadersSync['.js'], + }, + }; +};