Home Web Front-end JS Tutorial Using eslint configuration in webpack (detailed tutorial)

Using eslint configuration in webpack (detailed tutorial)

Jun 11, 2018 am 10:23 AM
eslint webpack

This article mainly introduces the detailed explanation of the eslint configuration introduced by webpack. Now I will share it with you and give you a reference.

Using eslint in webpack

First of all, to make webpack support eslint, you need to install eslint-loader. The command is as follows:

1

npm install --save-dev eslint-loader

Copy after login

In webpack Add the following code to .config.js:

1

2

3

4

5

6

7

8

9

{

  test: /\.js$/,

  loader: 'eslint-loader',

  enforce: "pre",

  include: [path.resolve(__dirname, 'src')], // 指定检查的目录

  options: { // 这里的配置项参数将会被传递到 eslint 的 CLIEngine

    formatter: require('eslint-friendly-formatter') // 指定错误报告的格式规范

  }

}

Copy after login

Note: The default formatter is stylish. If you want to use a third-party plug-in, you can install the plug-in, such as the eslint-friendly-formatter in the example above.

Secondly, if you want webpack to have eslint capabilities, you must install eslint. The command is as follows:

1

npm install --save-dev eslint

Copy after login

Finally, if the project wants to use those eslin rules, you can create a configuration file '.eslintrc .js', the code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

module.exports = {

  root: true, 

  parserOptions: {

    sourceType: 'module'

  },

  env: {

    browser: true,

  },

  rules: {

    "indent": ["error", 2],

    "quotes": ["error", "double"],

    "semi": ["error", "always"],

    "no-console": "error",

    "arrow-parens": 0

  }

}

Copy after login

In this way, a simple webpack introduction to eslint has been completed.

Here we will talk about the configuration and use of eslintrc.js. For details, please refer to http://eslint.cn/docs/user-guide

eslint configuration items

  1. root limits the usage scope of the configuration file

  2. parser specifies the eslint parser

  3. parserOptions sets parsing Device options

  4. extends specifies the eslint specification

  5. plugins refers to third-party plug-ins

  6. env specifies The host environment in which the code runs

  7. rules Enable additional rules or override the default rules

  8. globals Custom global variables declared in the code

When we use eslint, are the rules configuration items in the configuration file indispensable?

The answer is yes. However, we don’t need to customize reules. We can use third-party ones. Here we will use the extends configuration item.

extends

We can use the ones officially recommended by eslint, or we can use those provided by some big companies, such as: aribnb, google, standard.

We generally use third parties in development.

Official recommendation

Just add the following code in .eslintrc.js:

1

2

extends: 'eslint:recommended',

extends: 'eslint:all',

Copy after login

For details, please refer to the official rule table

Third-party sharing

To use third-party sharing, we generally need to install the relevant plug-in code as follows:

1

2

npm install --save-dev eslint-config-airbnb // bnb

npm install --save-dev eslint-config-standard // standard

Copy after login

Add the following code in .eslintrc.js:

1

2

3

extends: 'eslint:google',

// or

extends: 'eslint:standard',

Copy after login

Use these third parties extension, sometimes we need to update some plug-ins, such as standard: eslint-plugin-import

Don’t panic, we just need to install these plug-ins step by step according to the error prompts.

Although these third-party extensions are very good, sometimes we need to define some more personalized rules, and we need to add rules configuration items.

Configuration rules

Add rules in the .eslintrc.js file, the code is as follows:

1

2

3

4

5

6

{

  "rules": {

    "semi": ["error", "always"],

    "quotes": ["error", "double"]

  }

}

Copy after login

"semi" and "quotes" are the names of rules in ESLint. The first value is the error level, which can be one of the following:

  1. "off" or 0 - turns off the rule

  2. "warn " or 1 - treat the rule as a warning (will not affect the exit code)

  3. "error" or 2 - treat the rule as an error (exit code is 1)

These rules are generally divided into two categories:

  1. Add those not found in the default or third libraries

  2. Override the default or third library

There may be some other files in our project that also need to be formatted, such as: html, vue, react, etc. For the processing of these files , we need to introduce third-party plug-ins.

plugins (html)

Install eslint-plugin-html, the command is as follows:

1

npm install --save-dev eslint-plugin-html

Copy after login

This plug-in will prompt simulated browsing between module scripts This does not apply to module scripts as this does not apply to module scripts.

This plug-in can also extend files, such as: .vue, .jsx

In .eslintrc.js, add the following configuration items:

1

2

3

4

settings: {

  'html/html-extensions': ['.html', '.vue'],

  'html/indent': '+2',

},

Copy after login

For this kind of use eslint We can use eslint --ext .html,.vue src to detect files with -pulgin-html extension. If we want to detect while writing during development, we can use the loader of the corresponding file to process it. Then execute npm run dev to achieve the function. The function of checking while writing.

Sometimes during development, we may use different .eslintrc.js files in different directories of the same project as needed. In this case, we need to use the configuration item root.

Limit the scope of use (root: true)

If we want to use different .eslintrc in different directories, we need to add the following in the directory Configuration items:

1

2

3

{

  "root": true

}

Copy after login

If we do not set it, it will continue to search until the directory is updated. If there is a configuration file in the updated directory, it will use the root directory, which will cause the current configuration directory configuration to fail. function problem.

In development, we need to use different parsers for different situations, and the one we commonly use is babel-eslint.

parser (specify parser)

babel-eslint 解析器是一种使用频率很高的解析器,因为现在很多公司的很多项目目前都使用了es6,为了兼容性考虑基本都使用babel插件对代码进行编译。而用babel编译后的代码使用 babel-eslint 这款解析器可以避免不必要的麻烦。

babel-eslint 安装命令:

1

npm install --save-dev babel-eslint

Copy after login

在 .eslintrc.js 配置文件中添加如下配置项代码:

1

parser: 'babel-eslint',

Copy after login

如果你使用的默认解析器的话,且在代码中使用了浏览器有兼容性的问题的js新特性,使用webpack编译就会出现问题,这时我们一般装最新的eslint或者安装是使用 babel-eslint 来解决问题。

env(环境)

在 .eslintrc.js 中添加如下代码:

1

2

3

4

"env": {

  "browser": true, //

  "node": true //

}

Copy after login

指定了环境,你就可以放心的使用它们的全局变量和属性。

global

指定全局变量。

在 .eslintrc.js 中添加如下代码:

1

2

3

4

"globals": {

  "var1": true,

  "var2": false

}

Copy after login

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在JS中如何改变单物体透明度

在JS中如何实现通过拖拽改变物体大小

在vue中使用cli如何实现重构多页面脚手架

The above is the detailed content of Using eslint configuration in webpack (detailed tutorial). 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

VUE3 Getting Started Tutorial: Packaging and Building with Webpack VUE3 Getting Started Tutorial: Packaging and Building with Webpack Jun 15, 2023 pm 06:17 PM

Vue is an excellent JavaScript framework that can help us quickly build interactive and efficient web applications. Vue3 is the latest version of Vue, which introduces many new features and functionality. Webpack is currently one of the most popular JavaScript module packagers and build tools, which can help us manage various resources in our projects. This article will introduce how to use Webpack to package and build Vue3 applications. 1. Install Webpack

What is the difference between vite and webpack What is the difference between vite and webpack Jan 11, 2023 pm 02:55 PM

Differences: 1. The startup speed of the webpack server is slower than that of Vite; because Vite does not require packaging when starting, there is no need to analyze module dependencies and compile, so the startup speed is very fast. 2. Vite hot update is faster than webpack; in terms of HRM of Vite, when the content of a certain module changes, just let the browser re-request the module. 3. Vite uses esbuild to pre-build dependencies, while webpack is based on node. 4. The ecology of Vite is not as good as webpack, and the loaders and plug-ins are not rich enough.

How to delete eslint in react How to delete eslint in react Dec 30, 2022 am 09:46 AM

How to delete eslint from react: 1. Execute the "npm run eject" command; 2. Modify the code in package.json to ""eslintConfig": {"extends": ["react-app","react-app/jest" ],"rules": {"no-undef": "off"...}"; 3. Restart the project.

How to use PHP and webpack for modular development How to use PHP and webpack for modular development May 11, 2023 pm 03:52 PM

With the continuous development of web development technology, front-end and back-end separation and modular development have become a widespread trend. PHP is a commonly used back-end language. When doing modular development, we need to use some tools to manage and package modules. Webpack is a very easy-to-use modular packaging tool. This article will introduce how to use PHP and webpack for modular development. 1. What is modular development? Modular development refers to decomposing a program into different independent modules. Each module has its own function.

How does webpack convert es6 to es5 module? How does webpack convert es6 to es5 module? Oct 18, 2022 pm 03:48 PM

Configuration method: 1. Use the import method to put the ES6 code into the packaged js code file; 2. Use the npm tool to install the babel-loader tool, the syntax is "npm install -D babel-loader @babel/core @babel/preset- env"; 3. Create the configuration file ".babelrc" of the babel tool and set the transcoding rules; 4. Configure the packaging rules in the webpack.config.js file.

Use Spring Boot and Webpack to build front-end projects and plug-in systems Use Spring Boot and Webpack to build front-end projects and plug-in systems Jun 22, 2023 am 09:13 AM

As the complexity of modern web applications continues to increase, building excellent front-end engineering and plug-in systems has become increasingly important. With the popularity of Spring Boot and Webpack, they have become a perfect combination for building front-end projects and plug-in systems. SpringBoot is a Java framework that creates Java applications with minimal configuration requirements. It provides many useful features, such as automatic configuration, so that developers can build and deploy web applications faster and easier. W

What is Webpack? Detailed explanation of how it works? What is Webpack? Detailed explanation of how it works? Oct 13, 2022 pm 07:36 PM

Webpack is a module packaging tool. It creates modules for different dependencies and packages them all into manageable output files. This is especially useful for single-page applications (the de facto standard for web applications today).

What files can vue webpack package? What files can vue webpack package? Dec 20, 2022 pm 07:44 PM

In vue, webpack can package js, css, pictures, json and other files into appropriate formats for browser use; in webpack, js, css, pictures, json and other file types can be used as modules. Various module resources in webpack can be packaged and merged into one or more packages, and during the packaging process, the resources can be processed, such as compressing images, converting scss to css, converting ES6 syntax to ES5, etc., which can be recognized by HTML. file type.

See all articles