nx/packages/eslint/src/generators/lint-project/setup-root-eslint.ts
Jack Hsu b9c0e3db5f
feat(eslint): rename eslint.config.js to eslint.config.cjs to resolve them as CommonJS (#29334)
This PR updates our generators to use `eslint.config.cjs` instead of
`eslint.config.js` so that Node resolution will treat it as CommonJS.
This solves an issue where having `"type": "module"` in `package.json`
will result in an error when Node tries to resolve the config file as
ESM.

Also allows us to clean up out Remix generators to not have to rename to
`eslint.config.cjs` to solve the same issue.

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-12-18 16:34:10 -05:00

92 lines
2.4 KiB
TypeScript

import {
addDependenciesToPackageJson,
writeJson,
type GeneratorCallback,
type Tree,
} from '@nx/devkit';
import { useFlatConfig } from '../../utils/flat-config';
import {
eslint9__eslintVersion,
eslint9__typescriptESLintVersion,
eslintConfigPrettierVersion,
nxVersion,
typescriptESLintVersion,
} from '../../utils/versions';
import {
getGlobalEsLintConfiguration,
getGlobalFlatEslintConfiguration,
} from '../init/global-eslint-config';
import { findEslintFile } from '../utils/eslint-file';
export type SetupRootEsLintOptions = {
unitTestRunner?: string;
skipPackageJson?: boolean;
rootProject?: boolean;
};
export function setupRootEsLint(
tree: Tree,
options: SetupRootEsLintOptions
): GeneratorCallback {
const rootEslintFile = findEslintFile(tree);
if (rootEslintFile) {
return () => {};
}
if (!useFlatConfig(tree)) {
return setUpLegacyRootEslintRc(tree, options);
}
return setUpRootFlatConfig(tree, options);
}
function setUpLegacyRootEslintRc(tree: Tree, options: SetupRootEsLintOptions) {
writeJson(
tree,
'.eslintrc.json',
getGlobalEsLintConfiguration(options.unitTestRunner, options.rootProject)
);
if (tree.exists('.eslintignore')) {
let content = tree.read('.eslintignore', 'utf-8');
if (!/^node_modules$/gm.test(content)) {
content = `${content}\nnode_modules\n`;
tree.write('.eslintignore', content);
}
} else {
tree.write('.eslintignore', 'node_modules\n');
}
return !options.skipPackageJson
? addDependenciesToPackageJson(
tree,
{},
{
'@nx/eslint-plugin': nxVersion,
'@typescript-eslint/parser': typescriptESLintVersion,
'@typescript-eslint/eslint-plugin': typescriptESLintVersion,
'eslint-config-prettier': eslintConfigPrettierVersion,
}
)
: () => {};
}
function setUpRootFlatConfig(tree: Tree, options: SetupRootEsLintOptions) {
tree.write(
'eslint.config.cjs',
getGlobalFlatEslintConfiguration(options.rootProject)
);
return !options.skipPackageJson
? addDependenciesToPackageJson(
tree,
{},
{
'@eslint/js': eslint9__eslintVersion,
'@nx/eslint-plugin': nxVersion,
eslint: eslint9__eslintVersion,
'eslint-config-prettier': eslintConfigPrettierVersion,
'typescript-eslint': eslint9__typescriptESLintVersion,
}
)
: () => {};
}