Skip to content

Commit dcfa75c

Browse files
smeng9ArnaudBarrebluwy
authored
feat(create-vite): add eslint.config.js to React templates (#12860)
Co-authored-by: Shaoyu Meng <shaoyu> Co-authored-by: Arnaud Barré <[email protected]> Co-authored-by: bluwy <[email protected]>
1 parent 01f6cff commit dcfa75c

File tree

7 files changed

+110
-59
lines changed

7 files changed

+110
-59
lines changed

packages/create-vite/template-react-ts/.eslintrc.cjs

-18
This file was deleted.

packages/create-vite/template-react-ts/README.md

+31-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,37 @@ If you are developing a production application, we recommend updating the config
1414
- Configure the top-level `parserOptions` property like this:
1515

1616
```js
17-
export default {
18-
// other rules...
19-
parserOptions: {
20-
ecmaVersion: 'latest',
21-
sourceType: 'module',
22-
project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'],
23-
tsconfigRootDir: __dirname,
17+
export default tseslint.config({
18+
languageOptions: {
19+
// other options...
20+
parserOptions: {
21+
project: ['./tsconfig.node.json', './tsconfig.app.json'],
22+
tsconfigRootDir: import.meta.dirname,
23+
},
2424
},
25-
}
25+
})
2626
```
2727

28-
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
29-
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
30-
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
28+
- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
29+
- Optionally add `...tseslint.configs.stylisticTypeChecked`
30+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:
31+
32+
```js
33+
// eslint.config.js
34+
import react from 'eslint-plugin-react'
35+
36+
export default tseslint.config({
37+
// Set the react version
38+
settings: { react: { version: '18.3' } },
39+
plugins: {
40+
// Add the react plugin
41+
react,
42+
},
43+
rules: {
44+
// other rules...
45+
// Enable its recommended rules
46+
...react.configs.recommended.rules,
47+
...react.configs['jsx-runtime'].rules,
48+
},
49+
})
50+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import reactHooks from 'eslint-plugin-react-hooks'
4+
import reactRefresh from 'eslint-plugin-react-refresh'
5+
import tseslint from 'typescript-eslint'
6+
7+
export default tseslint.config({
8+
extends: [js.configs.recommended, ...tseslint.configs.recommended],
9+
files: ['**/*.{ts,tsx}'],
10+
ignores: ['dist'],
11+
languageOptions: {
12+
ecmaVersion: 2020,
13+
globals: globals.browser,
14+
},
15+
plugins: {
16+
'react-hooks': reactHooks,
17+
'react-refresh': reactRefresh,
18+
},
19+
rules: {
20+
...reactHooks.configs.recommended.rules,
21+
'react-refresh/only-export-components': [
22+
'warn',
23+
{ allowConstantExport: true },
24+
],
25+
},
26+
})

packages/create-vite/template-react-ts/package.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@
66
"scripts": {
77
"dev": "vite",
88
"build": "tsc -b && vite build",
9-
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
9+
"lint": "eslint .",
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
1313
"react": "^18.3.1",
1414
"react-dom": "^18.3.1"
1515
},
1616
"devDependencies": {
17+
"@eslint/js": "^9.5.0",
1718
"@types/react": "^18.3.3",
1819
"@types/react-dom": "^18.3.0",
19-
"@typescript-eslint/eslint-plugin": "^7.18.0",
20-
"@typescript-eslint/parser": "^7.18.0",
2120
"@vitejs/plugin-react": "^4.3.1",
22-
"eslint": "^8.57.0",
23-
"eslint-plugin-react-hooks": "^4.6.2",
24-
"eslint-plugin-react-refresh": "^0.4.9",
21+
"eslint": "^9.5.0",
22+
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
23+
"eslint-plugin-react-refresh": "^0.4.7",
24+
"globals": "^15.6.0",
2525
"typescript": "^5.5.3",
26+
"typescript-eslint": "^7.18.0",
2627
"vite": "^5.3.5"
28+
},
29+
"overrides": {
30+
"eslint": "$eslint"
2731
}
2832
}

packages/create-vite/template-react/.eslintrc.cjs

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import react from 'eslint-plugin-react'
4+
import reactHooks from 'eslint-plugin-react-hooks'
5+
import reactRefresh from 'eslint-plugin-react-refresh'
6+
7+
export default [
8+
{
9+
files: ['**/*.{js,jsx}'],
10+
ignores: ['dist'],
11+
languageOptions: {
12+
ecmaVersion: 2020,
13+
globals: globals.browser,
14+
parserOptions: {
15+
ecmaVersion: 'latest',
16+
ecmaFeatures: { jsx: true },
17+
sourceType: 'module',
18+
},
19+
},
20+
settings: { react: { version: '18.3' } },
21+
plugins: {
22+
react,
23+
'react-hooks': reactHooks,
24+
'react-refresh': reactRefresh,
25+
},
26+
rules: {
27+
...js.configs.recommended.rules,
28+
...react.configs.recommended.rules,
29+
...react.configs['jsx-runtime'].rules,
30+
...reactHooks.configs.recommended.rules,
31+
'react/jsx-no-target-blank': 'off',
32+
'react-refresh/only-export-components': [
33+
'warn',
34+
{ allowConstantExport: true },
35+
],
36+
},
37+
},
38+
]

packages/create-vite/template-react/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66
"scripts": {
77
"dev": "vite",
88
"build": "vite build",
9-
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
9+
"lint": "eslint .",
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
1313
"react": "^18.3.1",
1414
"react-dom": "^18.3.1"
1515
},
1616
"devDependencies": {
17+
"@eslint/js": "^9.5.0",
1718
"@types/react": "^18.3.3",
1819
"@types/react-dom": "^18.3.0",
1920
"@vitejs/plugin-react": "^4.3.1",
20-
"eslint": "^8.57.0",
21+
"eslint": "^9.5.0",
2122
"eslint-plugin-react": "^7.35.0",
22-
"eslint-plugin-react-hooks": "^4.6.2",
23+
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
2324
"eslint-plugin-react-refresh": "^0.4.9",
25+
"globals": "^15.6.0",
2426
"vite": "^5.3.5"
2527
}
2628
}

0 commit comments

Comments
 (0)