Skip to content

Commit fc37f5e

Browse files
author
pfdgitee
committed
fix: detect esmodule project
1 parent bd597ad commit fc37f5e

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

@commitlint/load/src/utils/load-config.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function loadConfig(
2727
return tsLoaderInstance(...args);
2828
};
2929

30-
const {searchPlaces, loaders} = getDynamicAwaitConfig();
30+
const {searchPlaces, loaders} = getDynamicAwaitConfig(cwd);
3131

3232
const explorer = cosmiconfig(moduleName, {
3333
searchPlaces: [
@@ -85,16 +85,38 @@ export const isDynamicAwaitSupported = () => {
8585

8686
// If dynamic await is supported (Node >= v20.8.0), support mjs config.
8787
// Otherwise, don't support mjs and use synchronous js/cjs loaders.
88-
export const getDynamicAwaitConfig = (): Partial<Options> =>
89-
isDynamicAwaitSupported()
90-
? {
91-
searchPlaces: [`.${moduleName}rc.mjs`, `${moduleName}.config.mjs`],
92-
loaders: {},
93-
}
94-
: {
88+
export const getDynamicAwaitConfig = (cwd?: string): Partial<Options> => {
89+
const dynamic = isDynamicAwaitSupported();
90+
if (dynamic) {
91+
return {
92+
searchPlaces: [`.${moduleName}rc.mjs`, `${moduleName}.config.mjs`],
93+
loaders: {},
94+
};
95+
}
96+
97+
if (cwd) {
98+
let type = null;
99+
try {
100+
const manifestPath = path.join(cwd, 'package.json');
101+
type = require(manifestPath).type;
102+
} catch (e) {
103+
// Do nothing
104+
}
105+
if (type === 'module') {
106+
return {
95107
searchPlaces: [],
96108
loaders: {
97109
'.cjs': defaultLoadersSync['.cjs'],
98-
'.js': defaultLoadersSync['.js'],
99110
},
100-
};
111+
};
112+
}
113+
}
114+
115+
return {
116+
searchPlaces: [],
117+
loaders: {
118+
'.cjs': defaultLoadersSync['.cjs'],
119+
'.js': defaultLoadersSync['.js'],
120+
},
121+
};
122+
};

0 commit comments

Comments
 (0)