Home Web Front-end JS Tutorial Detailed explanation of create-react-app configuration eslint steps

Detailed explanation of create-react-app configuration eslint steps

Jun 09, 2018 am 11:49 AM
eslint react Configuration

This time I will bring you a detailed explanation of the steps for configuring eslint in create-react-app. What are the precautions for configuring eslint in create-react-app? The following is a practical case, let's take a look.

Use eslint and editorconfig to standardize code

Why use these:

  1. Code specifications are conducive to team collaboration

  2. Purely manual specifications are time-consuming and labor-intensive and cannot guarantee accuracy

  3. Can cooperate with the editor to automatically remind errors and improve development efficiency

eslint

As the ECMAScript version is constantly updated, the Js lint tool has rich plug-ins and can apply specifications. The rules are very rich and can meet the needs of most teams.

eslint cooperates with git

In order to control everyone's specifications to the greatest extent, we can use git hook to call eslint for code specification verification when git commits the code. Canonical code cannot be submitted to the warehouse.

editorconfig

Different editors will have certain differences in the format of text. If some standards are not unified, you may update others every time you cooperate with others. There will be a lot of errors in the code - webstorm automatically supports the editorconfig configuration file.

First installeslintnpm i eslint Because create-react-app is already installed by default

  "babel-eslint": "7.2.3",
  "eslint": "4.10.0",
  "eslint-config-react-app": "^2.1.0",
  "eslint-loader": "1.9.0",
  "eslint-plugin-flowtype": "2.39.1",
  "eslint-plugin-import": "2.8.0",
  "eslint-plugin-jsx-a11y": "5.1.1",
  "eslint-plugin-react": "7.4.0",
Copy after login

For the custom configuration we want, we install it as follows

npm i babel-eslint \
\ eslint-config-airbnb eslint-config-standard \
\ eslint-loader \
\ eslint-plugin-import \
\ eslint-plugin-jsx-a11y \
\ eslint-plugin-node \
\ eslint-plugin-promise \
\ eslint-plugin-react \
\ eslint-plugin-standard -D
Copy after login

We create a new .eslintrc file in the root directory to make a standard specification for the entire project

{
 "extends": "standard"
}
Copy after login

The main project is the front-end project, so we create a new .eslintrc file in the front-end folder, go here Standardize client code. Client code uses jsx. Many rules are different from nodejs. Here, more strict specifications are used to require client code.

The value corresponding to the configured value: 0: off 1: warning 2: error

{
 "parser": "babel-eslint",
 "env": {
  "browser": true,
  "es6": true,
  "node": true
 },
 "parserOptions": {
  "ecmaVersion": 6,
  "sourceType": "module"
 },
 "extends": "airbnb",
 "rules": {
  "semi": [0],
  "react/jsx-filename-extension": [0],
  "jsx-a11y/anchor-is-valid": [0]
 }
}
Copy after login

Use babel-eslint to parse the code. The definition environment is browser and es6, which will contain public variables. Webpack therefore needs some environment variables of node, parserOptions to define the version, and jsmodule mode method writing.

Because you need to check the code before each compilation, you also need to configure webpack, which is the default of create-react-app

   {
    test: /\.(js|jsx|mjs)$/,
    enforce: 'pre',
    use: [
     {
      options: {
       formatter: eslintFormatter,
       eslintPath: require.resolve('eslint'),
       
      },
      loader: require.resolve('eslint-loader'),
     },
    ],
    include: paths.appSrc,
   },
Copy after login

We hope to block the node_modules folder The code

exclude:[path.resolve(__dirname, '../node_modules')]
Copy after login

Create a new file .editorconfig in the project root directory. Webstom has the configuration by default

  1. root = true Project root directory

  2. [*] This rule applies to all files

  3. charset = utf-8 encoding mode

  4. indent_style = space uses tab style Tab characters and space

  5. indent_size = 2

  6. ##end_of_line = lf line ending method

  7. insert_final_newline = true Automatically save the last line at the end of the line as a blank line

  8. trim_trailing_whitespace = true Automatically remove the spaces after the end of a line

eslint does not detect This line of code

// eslint-disable-line

eslint does not detect this file, at the beginning

/* eslint-disable */ at the end of the file / * eslint-enable */

Install

npm i husky -D

Then add the git hook in package.json scripts, which will be executed before executing git commit Call this command

"lint": "eslint --ext .js --ext .jsx src/",
"precommit": "npm run lint"
Copy after login
git commit to force the execution of eslint passed code

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use the entry component

How to use the Installation plug-in in actual projects

The above is the detailed content of Detailed explanation of create-react-app configuration eslint steps. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

How to configure and install FTPS in Linux system How to configure and install FTPS in Linux system Mar 20, 2024 pm 02:03 PM

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Computer configuration recommendations for building a high-performance Python programming workstation Computer configuration recommendations for building a high-performance Python programming workstation Mar 25, 2024 pm 07:12 PM

Title: Computer configuration recommendations for building a high-performance Python programming workstation. With the widespread application of the Python language in data analysis, artificial intelligence and other fields, more and more developers and researchers have an increasing demand for building high-performance Python programming workstations. When choosing a computer configuration, in addition to performance considerations, it should also be optimized according to the characteristics of Python programming to improve programming efficiency and running speed. This article will introduce how to build a high-performance Python programming workstation and provide specific

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Common problems and solutions for Laravel environment configuration file .env Common problems and solutions for Laravel environment configuration file .env Mar 10, 2024 pm 12:51 PM

Common problems and solutions for Laravel environment configuration file .env When using the Laravel framework to develop projects, the environment configuration file .env is very important. It contains key configuration information of the project, such as database connection information, application keys, etc. However, sometimes there are some common problems when configuring the .env file. This article will introduce these problems and provide solutions, and attach specific code examples for reference. Problem 1: Unable to read the .env file when we have configured the .env file

See all articles