In my monorepo, each package currently contains a .eslintrc.cjs
file with the following setup:
Package-specific ESLint Configuration
const path = require('path')
const ts = require('typescript')
const OFF = 0
const WARN = 1
const ERROR = 2
const tsConfigPath = path.resolve(__dirname, 'tsconfig.json')
const pathAliases = Object.keys(
ts.readConfigFile(tsConfigPath, ts.sys.readFile)?.config?.compilerOptions?.paths ?? []
)
?.map((key) => key.replace(/[/*@]/g, ''))
?.sort()
?.join('|')
/**
* @type {import('eslint').Linter.Config}
* @see https://eslint.org/docs/user-guide/configuring
**/
module.exports = {
rules: {
'simple-import-sort/imports': [
ERROR,
{
groups: [
['^@?\\w'],
pathAliases.length ? [`^@?(${pathAliases})(/.*|$)`] : [],
['^\\u0000'],
['^\\.\\.(?!/?$)', '^\\.\\./?$'],
['^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$'],
],
},
],
},
}
Root ESLint Configuration
const OFF = 0
const WARN = 1
const ERROR = 2
/**
* @type {import('eslint').Linter.Config}
* @see https://eslint.org/docs/user-guide/configuring
**/
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:jest-formatting/recommended',
'plugin:sonarjs/recommended',
'plugin:import/typescript',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: [
'./tsconfig.browser.base.json',
'./tsconfig.browser.base.json',
'./packages/service/*/tsconfig.json',
'./packages/utility/*/tsconfig.json',
'./packages/middleware/*/tsconfig.json',
],
tsconfigRootDir: __dirname,
ecmaFeatures: {
impliedStrict: true,
},
},
plugins: [],
rules: {},
}
This setup retrieves the pathAliases
from the TSConfig and integrates it into the simple-import-sort
ESLint rule. I am exploring ways to consolidate this configuration into the root eslint config so that each linted file can access the relative tsconfig paths
property efficiently.