Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect esmodule project #3773

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions @commitlint/load/src/utils/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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<Options> =>
isDynamicAwaitSupported()
? {
searchPlaces: [`.${moduleName}rc.mjs`, `${moduleName}.config.mjs`],
loaders: {},
}
: {
export const getDynamicAwaitConfig = (cwd?: string): Partial<Options> => {
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'],
},
};
};