Using eslint configuration in webpack (detailed tutorial)
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 |
|
In webpack Add the following code to .config.js:
1 2 3 4 5 6 7 8 9 |
|
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 |
|
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 |
|
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
root limits the usage scope of the configuration file
parser specifies the eslint parser
parserOptions sets parsing Device options
extends specifies the eslint specification
plugins refers to third-party plug-ins
env specifies The host environment in which the code runs
rules Enable additional rules or override the default rules
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 |
|
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 |
|
Add the following code in .eslintrc.js:
1 2 3 |
|
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 |
|
"semi" and "quotes" are the names of rules in ESLint. The first value is the error level, which can be one of the following:
"off" or 0 - turns off the rule
"warn " or 1 - treat the rule as a warning (will not affect the exit code)
"error" or 2 - treat the rule as an error (exit code is 1)
These rules are generally divided into two categories:
Add those not found in the default or third libraries
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 |
|
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 |
|
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 |
|
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 |
|
在 .eslintrc.js 配置文件中添加如下配置项代码:
1 |
|
如果你使用的默认解析器的话,且在代码中使用了浏览器有兼容性的问题的js新特性,使用webpack编译就会出现问题,这时我们一般装最新的eslint或者安装是使用 babel-eslint 来解决问题。
env(环境)
在 .eslintrc.js 中添加如下代码:
1 2 3 4 |
|
指定了环境,你就可以放心的使用它们的全局变量和属性。
global
指定全局变量。
在 .eslintrc.js 中添加如下代码:
1 2 3 4 |
|
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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 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.

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.

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.

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

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).

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.
