Skip to content

Commit d07b7d0

Browse files
tooppaaamrmckeb
andauthored
Replace deprecated eslint-loader with eslint-webpack-plugin (#9751)
* Replace deprecated eslint-loader by eslint-webpack-plugin * Update eslintFormatter for eslint-webpack-plugin * fix: always enable jsx-uses-react Co-authored-by: Brody McKee <mrmckeb@hotmail.com>
1 parent 6f3e32e commit d07b7d0

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

Diff for: packages/eslint-config-react-app/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ module.exports = {
5151

5252
rules: {
5353
'react/jsx-uses-vars': 'warn',
54+
'react/jsx-uses-react': 'warn',
5455
...(!hasJsxRuntime && {
55-
'react/jsx-uses-react': 'warn',
5656
'react/react-in-jsx-scope': 'error',
5757
}),
5858
},

Diff for: packages/react-dev-utils/eslintFormatter.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77

88
'use strict';
99

10+
const path = require('path');
1011
const chalk = require('chalk');
1112
const stripAnsi = require('strip-ansi');
1213
const table = require('text-table');
1314

15+
const cwd = process.cwd();
16+
1417
function isError(message) {
1518
if (message.fatal || message.severity === 2) {
1619
return true;
1720
}
1821
return false;
1922
}
2023

24+
function getRelativePath(filePath) {
25+
return path.relative(cwd, filePath);
26+
}
27+
2128
function formatter(results) {
2229
let output = '\n';
2330
let hasErrors = false;
@@ -73,6 +80,10 @@ function formatter(results) {
7380
},
7481
});
7582

83+
// print the filename and relative path
84+
output += `${getRelativePath(result.filePath)}\n`;
85+
86+
// print the errors
7687
output += `${outputTable}\n\n`;
7788
});
7889

Diff for: packages/react-scripts/config/webpack.config.js

+14-23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
2626
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
2727
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
2828
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
29+
const ESLintPlugin = require('eslint-webpack-plugin');
2930
const paths = require('./paths');
3031
const modules = require('./modules');
3132
const getClientEnvironment = require('./env');
@@ -349,29 +350,6 @@ module.exports = function (webpackEnv) {
349350
rules: [
350351
// Disable require.ensure as it's not a standard language feature.
351352
{ parser: { requireEnsure: false } },
352-
353-
// First, run the linter.
354-
// It's important to do this before Babel processes the JS.
355-
{
356-
test: /\.(js|mjs|jsx|ts|tsx)$/,
357-
enforce: 'pre',
358-
use: [
359-
{
360-
options: {
361-
cache: true,
362-
cwd: paths.appPath,
363-
formatter: require.resolve('react-dev-utils/eslintFormatter'),
364-
eslintPath: require.resolve('eslint'),
365-
resolvePluginsRelativeTo: __dirname,
366-
baseConfig: {
367-
extends: [require.resolve('eslint-config-react-app/base')],
368-
},
369-
},
370-
loader: require.resolve('eslint-loader'),
371-
},
372-
],
373-
include: paths.appSrc,
374-
},
375353
{
376354
// "oneOf" will traverse all following loaders until one will
377355
// match the requirements. When no loader matches it will fall
@@ -742,6 +720,19 @@ module.exports = function (webpackEnv) {
742720
// The formatter is invoked directly in WebpackDevServerUtils during development
743721
formatter: isEnvProduction ? typescriptFormatter : undefined,
744722
}),
723+
new ESLintPlugin({
724+
// Plugin options
725+
extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
726+
formatter: require.resolve('react-dev-utils/eslintFormatter'),
727+
eslintPath: require.resolve('eslint'),
728+
context: paths.appSrc,
729+
// ESLint class options
730+
cwd: paths.appPath,
731+
resolvePluginsRelativeTo: __dirname,
732+
baseConfig: {
733+
extends: [require.resolve('eslint-config-react-app/base')],
734+
},
735+
}),
745736
].filter(Boolean),
746737
// Some libraries import Node modules but don't use them in the browser.
747738
// Tell webpack to provide empty mocks for them so importing them works.

Diff for: packages/react-scripts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@
4646
"dotenv-expand": "5.1.0",
4747
"eslint": "^7.9.0",
4848
"eslint-config-react-app": "^5.2.1",
49-
"eslint-loader": "^4.0.2",
5049
"eslint-plugin-flowtype": "^5.2.0",
5150
"eslint-plugin-import": "^2.22.0",
5251
"eslint-plugin-jest": "^24.0.1",
5352
"eslint-plugin-jsx-a11y": "^6.3.1",
5453
"eslint-plugin-react": "^7.20.6",
5554
"eslint-plugin-react-hooks": "^4.1.2",
55+
"eslint-webpack-plugin": "^2.1.0",
5656
"file-loader": "6.1.0",
5757
"fs-extra": "^9.0.0",
5858
"html-webpack-plugin": "4.4.1",

0 commit comments

Comments
 (0)