Skip to content

Commit

Permalink
Allow extending config used in eslint-loader (#7036)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb authored Jul 16, 2019
1 parent 3a9b87b commit 3495286
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
1 change: 1 addition & 0 deletions docusaurus/docs/advanced-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ You can adjust various development and production settings by setting environmen
| NODE_PATH | ✅ Used | ✅ Used | Same as [`NODE_PATH` in Node.js](https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders), but only relative folders are allowed. Can be handy for emulating a monorepo setup by setting `NODE_PATH=src`. |
| INLINE_RUNTIME_CHUNK | 🚫 Ignored | ✅ Used | By default, Create React App will embed the runtime script into `index.html` during the production build. When set to `false`, the script will not be embedded and will be imported as usual. This is normally required when dealing with CSP. |
| IMAGE_INLINE_SIZE_LIMIT | 🚫 Ignored | ✅ Used | By default, images smaller than 10,000 bytes are encoded as a data URI in base64 and inlined in the CSS or JS build artifact. Set this to control the size limit in bytes. Setting it to 0 will disable the inlining of images. |
| EXTEND_ESLINT | ✅ Used | ✅ Used | When set to `true`, ESLint configs that extend `eslint-config-react-app` will be used by `eslint-loader`. Any rules that are set to `"error"` will stop the application from building. |
48 changes: 38 additions & 10 deletions docusaurus/docs/setting-up-your-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ To configure the syntax highlighting in your favorite text editor, head to the [
Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint.

They are not required for linting. You should see the linter output right in your terminal as well as the browser console. However, if you prefer the lint results to appear right in your editor, there are some extra steps you can do.

You would need to install an ESLint plugin for your editor first. Then, add a file called `.eslintrc.json` to the project root:

```json
{
"extends": "react-app"
}
```
They are not required for linting. You should see the linter output right in your terminal as well as the browser console. If you prefer the lint results to appear right in your editor, please make sure you install an ESLint plugin/extension.

If you're using TypeScript and Visual Studio Code, the [ESLint Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint#overview) currently [doesn't have TypeScript support enabled by default](https://github.com/Microsoft/vscode-eslint/issues/609). To enable TypeScript support in the ESLint extension, add the following to your project's Visual Studio Code settings file, located at `.vscode/settings.json` (you can create this file if it doesn't already exist):

Expand All @@ -43,10 +35,46 @@ If you're using TypeScript and Visual Studio Code, the [ESLint Visual Studio Cod

Now your editor should report the linting warnings.

Note that even if you edit your `.eslintrc.json` file further, these changes will **only affect the editor integration**. They won’t affect the terminal and in-browser lint output. This is because Create React App intentionally provides a minimal set of rules that find common mistakes.
Note that even if you customise your ESLint config, these changes will **only affect the editor integration**. They won’t affect the terminal and in-browser lint output. This is because Create React App intentionally provides a minimal set of rules that find common mistakes.

If you want to enforce a coding style for your project, consider using [Prettier](https://github.com/jlongster/prettier) instead of ESLint style rules.

### Experimental: Extending the ESLint config

We recognise that in some cases, further customisation is required. It is now possible to extend the base ESLint config by setting the `EXTEND_ESLINT` environment variable to `true`. See [advanced configuration](advanced-configuration.md) for more information on available environment variables.

Note that any rules set to `"error"` will stop the project from building.

There are a few things to remember:

1. You must extend the base config, as removing it could introduce hard-to-find issues.
1. When working with TypeScript, you'll need to provide an `overrides` object for rules that should _only_ target TypeScript files.

In the below example:

- the base config has been extended by a shared ESLint config,
- a new rule has been set that applies to all JavaScript and TypeScript files, and
- a new rule has been set that only targets TypeScript files.

```json
{
"eslintConfig": {
"extends": ["react-app", "shared-config"],
"rules": {
"additional-rule": "warn"
},
"overrides": [
{
"files": ["**/*.ts?(x)"],
"rules": {
"additional-typescript-only-rule": "warn"
}
}
]
}
}
```

## Debugging in the Editor

**This feature is currently only supported by [Visual Studio Code](https://code.visualstudio.com) and [WebStorm](https://www.jetbrains.com/webstorm/).**
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {

overrides: [
{
files: ['**/*.ts', '**/*.tsx'],
files: ['**/*.ts?(x)'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
Expand Down
28 changes: 25 additions & 3 deletions packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const getClientEnvironment = require('./env');
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin');
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
const eslint = require('eslint');
// @remove-on-eject-begin
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
// @remove-on-eject-end
Expand Down Expand Up @@ -323,9 +324,30 @@ module.exports = function(webpackEnv) {
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
// @remove-on-eject-begin
baseConfig: {
extends: [require.resolve('eslint-config-react-app')],
},
baseConfig: (() => {
const eslintCli = new eslint.CLIEngine();
let eslintConfig;
try {
eslintConfig = eslintCli.getConfigForFile(paths.appIndexJs);
} catch (e) {
// A config couldn't be found.
}

// We allow overriding the config, only if it extends our config
// (`extends` can be a string or array of strings).
if (
process.env.EXTEND_ESLINT &&
eslintConfig &&
eslintConfig.extends &&
eslintConfig.extends.includes('react-app')
) {
return eslintConfig;
} else {
return {
extends: [require.resolve('eslint-config-react-app')],
};
}
})(),
ignore: false,
useEslintrc: false,
// @remove-on-eject-end
Expand Down
10 changes: 6 additions & 4 deletions packages/react-scripts/scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ inquirer
};

// Add ESlint config
console.log(` Adding ${cyan('ESLint')} configuration`);
appPackage.eslintConfig = {
extends: 'react-app',
};
if (!appPackage.eslintConfig) {
console.log(` Adding ${cyan('ESLint')} configuration`);
appPackage.eslintConfig = {
extends: 'react-app',
};
}

fs.writeFileSync(
path.join(appPath, 'package.json'),
Expand Down

0 comments on commit 3495286

Please sign in to comment.