This pull request includes changes to migrate ESLint configuration files
from CommonJS (`.cjs`) to ECMAScript modules (`.mjs`) as the default.
### ESLint Configuration Generation Changes
The changes also ensure consistent generated eslint configs based on the
base eslint config.
- If the workspace root has an `eslint.config.cjs` or `eslint.config.js`
with `module.exports`. When you create a library or application it will
generate an accompanying config at path
`{projectRoot}/eslint.config.cjs` of the same format.
- If the workspace root has an `eslint.config.mjs` or
`eslint.config.mjs` with `export default`. When you create a library or
application it will generate an accompanying config at path
`{projectRoot}/eslint.config.mjs`.
- If no eslint config is found at the workspace root one will be created
`eslint.config.mjs`
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { Tree } from '@nx/devkit';
|
|
import { gte } from 'semver';
|
|
|
|
export const eslintFlatConfigFilenames = [
|
|
'eslint.config.cjs',
|
|
'eslint.config.js',
|
|
'eslint.config.mjs',
|
|
];
|
|
|
|
export const baseEslintConfigFilenames = [
|
|
'eslint.base.js',
|
|
'eslint.base.config.cjs',
|
|
'eslint.base.config.js',
|
|
'eslint.base.config.mjs',
|
|
];
|
|
|
|
export function getRootESLintFlatConfigFilename(tree: Tree): string {
|
|
for (const file of eslintFlatConfigFilenames) {
|
|
if (tree.exists(file)) {
|
|
return file;
|
|
}
|
|
}
|
|
throw new Error('Could not find root flat config file');
|
|
}
|
|
|
|
export function useFlatConfig(tree?: Tree): boolean {
|
|
// Prioritize taking ESLint's own environment variable into account when determining if we should use flat config
|
|
// If it is not defined, then default to true.
|
|
if (process.env.ESLINT_USE_FLAT_CONFIG === 'true') {
|
|
return true;
|
|
} else if (process.env.ESLINT_USE_FLAT_CONFIG === 'false') {
|
|
return false;
|
|
}
|
|
|
|
// If we find an existing flat config file in the root of the provided tree, we should use flat config
|
|
if (tree) {
|
|
const hasRootFlatConfig = eslintFlatConfigFilenames.some((filename) =>
|
|
tree.exists(filename)
|
|
);
|
|
if (hasRootFlatConfig) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Otherwise fallback to checking the installed eslint version
|
|
try {
|
|
const { ESLint } = require('eslint');
|
|
// Default to any v8 version to compare against in this case as it implies a much older version of ESLint was found (and gte() requires a valid version)
|
|
const eslintVersion = ESLint.version || '8.0.0';
|
|
return gte(eslintVersion, '9.0.0');
|
|
} catch {
|
|
// Default to assuming flat config in case ESLint is not yet installed
|
|
return true;
|
|
}
|
|
}
|