Web项目如何配置Eslint
介绍
ESLint 是一个根据方案识别并报告 ECMAScript/JavaScript 代码问题的工具,其目的是使代码风格更加一致并避免错误。在很多地方它都与 JSLint 和 JSHint 类似,除了:
- ESLint 使用 Espree 对 JavaScript 进行解析。
- ESLint 在代码中使用 AST 评估方案。
- ESLint 完全是插件式的,每个规则都是一个插件,你可以在运行时中添加更多插件。
安装
@REM npm 安装eslint
npm i -D eslint
@REM yarn 安装eslint
yarn add -D eslint
配置
配置过程
npm init @eslint/config
You can also run this command directly using 'npm init @eslint/config'.
Need to install the following packages:
@eslint/create-config@0.4.6
Ok to proceed? (y) y
@REM 您还可以使用“npm init @eslint/config”直接运行此命令。
@REM 需要安装以下软件包:
@REM @eslint/create-config@0.4.6
@REM 确定继续: 是
How would you like to use ESLint? ...
To check syntax only
To check syntax and find problems
> To check syntax, find problems, and enforce code style
@REM 您想如何使用 ESLint? ...
@REM 仅检查语法
@REM 检查语法并发现问题
@REM > 检查语法、发现问题并强制执行代码风格
What type of modules does your project use? ...
> JavaScript modules (import/export)
CommonJS (require/exports)
None of these
@REM 您的项目使用什么类型的模块? ...
@REM > JavaScript 模块(导入/导出)
@REM CommonJS(要求/导出)
@REM 都不是
Which framework does your project use? ...
React
> Vue.js
None of these
@REM 您的项目使用哪个框架? ...
@REM React
@REM > Vue.js
@REM 都不是
Does your project use TypeScript? » No / Yes
@REM 您的项目使用 TypeScript 吗? » 否/是
Where does your code run? ... (Press <space> to select, <a> to toggle all, <i> to invert selection)
√ Browser
√ Node
你的代码在哪里运行? ...(按 <space> 进行选择,<a> 切换全部,<i> 反转选择)
√ 浏览器
√ 节点
How would you like to define a style for your project? ...
> Use a popular style guide
Answer questions about your style
@REM 您希望如何为您的项目定义风格? ...
@REM > 使用流行的风格指南
@REM 回答有关您风格的问题
Which style guide do you want to follow? ...
Airbnb: https://github.com/airbnb/javascript
> Standard: https://github.com/standard/standard
Google: https://github.com/google/eslint-config-google
XO: https://github.com/xojs/eslint-config-xo
@REM 您想遵循哪种风格指南? ...
@REM Airbnb: https://github.com/airbnb/javascript
@REM > Standard: https://github.com/standard/standard
@REM Google: https://github.com/google/eslint-config-google
@REM XO: https://github.com/xojs/eslint-config-xo
What format do you want your config file to be in? ...
> JavaScript
YAML
JSON
@REM 您希望配置文件采用什么格式? ...
@REM > JavaScript
@REM YAML
@REM JSON
@REM 您选择的配置需要以下依赖项:
@REM eslint-plugin-vue@latest eslint-config-standard@latest eslint@^8.0.1 eslint-plugin-import@^2.25.2 eslint-plugin-n@^15.0.0 || ^16.0.0 eslint-plugin-promise@^6.0.0
@REM ?您想现在安装它们吗? » 否/是
The config that you've selected requires the following dependencies:
eslint-plugin-vue@latest eslint-config-standard@latest eslint@^8.0.1 eslint-plugin-import@^2.25.2 eslint-plugin-n@^15.0.0 || ^16.0.0 eslint-plugin-promise@^6.0.0
? Would you like to install them now? » No / Yes
Which package manager do you want to use? ...
> npm
yarn
pnpm
@REM 您想使用哪个包管理器? ...
@REM > npm
@REM yarn
@REM pnpm
配置结果
√ How would you like to use ESLint? · style
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ How would you like to define a style for your project? · guide
√ Which style guide do you want to follow? · standard
√ What format do you want your config file to be in? · JavaScript
Checking peerDependencies of eslint-config-standard@latest
The config that you've selected requires the following dependencies:
eslint-plugin-vue@latest eslint-config-standard@latest eslint@^8.0.1 eslint-plugin-import@^2.25.2 eslint-plugin-n@^15.0.0 || ^16.0.0 eslint-plugin-promise@^6.0.0
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · npm
Installing eslint-plugin-vue@latest, eslint-config-standard@latest, eslint@^8.0.1, eslint-plugin-import@^2.25.2, eslint-plugin-n@^15.0.0 || ^16.0.0 , eslint-plugin-promise@^6.0.0
安装项目 Eslint 支持
Webpack 方式
安装 eslint-webpack-plugin
@REM npm
npm install eslint-webpack-plugin -D
@REM yarn
yarn add eslint-webpack-plugin --save-dev
在 Webpack.config.js 中使用
详细配置可查看官网:eslint-webpack-plugin
plugins: [
new ESLintWebpackPlugin({
// 指定检查文件的根目录
context: path.resolve(__dirname, "src")
})
],
Vite 方式
其实, Viite 创建的项目本来是不需要另外配置 Eslint 根据 Vite 官网文档,一般在使用 Vite 的命令创建 Web 项目时使用自定义创建使用不使用默认模版时会询问是否需要配置 Eslint,但是在使用脚本创建 uniapp 项目时需要另外安装
使用 Vite 建立项目例子
npm create vite@latest my-vue-app --template vue
√ Select a framework: » Vue
√ Select a variant: » Customize with create-vue
Need to install the following packages:
create-vue@3.7.3
Ok to proceed? (y) y
Vue.js - The Progressive JavaScript Framework
√ Add TypeScript? ... No / Yes
√ Add JSX Support? ... No / Yes
√ Add Vue Router for Single Page Application development? ... No / Yes
√ Add Pinia for state management? ... No / Yes
√ Add Vitest for Unit Testing? ... No / Yes
√ Add an End-to-End Testing Solution? » No
√ Add ESLint for code quality? ... No / Yes
√ Add Prettier for code formatting? ... No / Yes
安装 vite-plugin-eslint
说明: 该包是用于配置 vite 运行的时候自动检测 eslint 规范 不符合页面会报错
@REG npm 安装
npm i -D vite-plugin-eslint
@REG yarn 安装
yarn add -D vite-plugin-eslint --save-dev
安装 eslint-parser
@REM npm 安装
npm i -D @babel/core @babel/eslint-parser
@REM yarn 安装
yarn add @babel/core @babel/eslint-parser --save-dev
拓展(安装 prettier)
正常来说安装到上一步已经是完成了 Eslint 配置,此时使用的是 Eslint 流行风格中的 Standard 风格,由于该规则比较严格,使用起来可能有点麻烦,有些开发者就喜欢使用 prettier 的相对比较宽松的规则,以下就是该规则的配置方式
安装依赖
@REM npm 安装
npm i -D prettier eslint-config-prettier eslint-plugin-prettier
@REM yarn 安装
yarn add prettier eslint-config-prettier eslint-plugin-prettier --save-dev
配置.prettierrc.js
在根目录下面添加.prettierrc.js 文件,然后将下面的配置添加到其中:
//这些配置随项目规范而定,并非需要
module.exports = {
// 一行最多多少个字符
printWidth: 150,
// 指定每个缩进级别的空格数
tabWidth: 2,
// 使用制表符而不是空格缩进行
useTabs: true,
// 在语句末尾是否需要分号
semi: true,
// 是否使用单引号
singleQuote: true,
// 更改引用对象属性的时间 可选值"<as-needed|consistent|preserve>"
quoteProps: "as-needed",
// 在JSX中使用单引号而不是双引号
jsxSingleQuote: false,
// 多行时尽可能打印尾随逗号。(例如,单行数组永远不会出现逗号结尾。) 可选值"<none|es5|all>",默认none
trailingComma: "es5",
// 在对象文字中的括号之间打印空格
bracketSpacing: true,
// jsx 标签的反尖括号需要换行
jsxBracketSameLine: false,
// 在单独的箭头函数参数周围包括括号 always:(x) => x \ avoid:x => x
arrowParens: "always",
// 这两个选项可用于格式化以给定字符偏移量(分别包括和不包括)开始和结束的代码
rangeStart: 0,
rangeEnd: Infinity,
// 指定要使用的解析器,不需要写文件开头的 @prettier
requirePragma: false,
// 不需要自动在文件开头插入 @prettier
insertPragma: false,
// 使用默认的折行标准 always\never\preserve
proseWrap: "preserve",
// 指定HTML文件的全局空格敏感度 css\strict\ignore
htmlWhitespaceSensitivity: "css",
// Vue文件脚本和样式标签缩进
vueIndentScriptAndStyle: false,
endOfLine: "auto",
};
注意:配置文件修改如果是 eslint 能立刻反应并且提示,但是 prettier 修改后,由于结合了 eslint 插件,可能会导致不同步,需要重启 VsCode 才能一致,比如把单引号修改为双引号,prettier 能立马格式化代码使用双引号,但是 eslint 检查会报错,所以修改完成 prettier 配置文件建议重启一下 VsCode,保证 eslint 检查同步更新。
配置.prettierignore
如果不想格式化某些文件可以再添加一个的文件,用法和.gitignore 文件差不多,将不需要格式化的文件夹或文件通过正则匹配或者具名的方式添加进去,这样就不会格式化对应的文件了。
Prettier 结合 ESLint
在 package.json 的 scripts 节点中添加如下节点
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore"
在.eslintrc.js 在添加如下选项,然后运行 npm run lint 命令即可生效
"plugin:prettier/recommended", "prettier";
Web项目如何配置Eslint的更多相关文章
- 搭建maven web项目并配置quartz定时任务【业务:对比数据变化内容】 历程
搭建maven web项目并配置quartz定时任务[业务:对比数据变化内容] 历程2018年03月03日 10:51:10 守望dfdfdf 阅读数:100更多个人分类: 工作 问题编辑版权声明:本 ...
- web项目中配置多个数据源
web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源, ...
- 如何在web项目中配置Spring的Ioc容器
在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...
- 当web项目没有配置<welcome-file>index_1.jsp</welcome-file>默认标签启动tomcat后默认访问的页面是什么呢?
当web项目没有配置index_1.jsp默认标签启动tomcat后默认访问的页面是什么呢? 结果我启动后居然默认打开了index.jsp页面 为什么呢?为什么会访问我的.indexjsp页面呢?不是 ...
- Log4j2在WEB项目中配置
最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...
- 《转载》IntelliJ 2016.2 IDEA 中进行 Java Web 项目开发配置
本文转载自 https://segmentfault.com/a/1190000007088964 1. 新建一个 Web Application 项目 打开 IntelliJ,选择新建项目: 左边栏 ...
- Maven 创建java Web项目,配置Spring,CXF
1.搭建Maven环境 参考文章 Maven3路程(一)环境搭建 http://www.cnblogs.com/leiOOlei/p/3359561.html Maven3路程(二)Eclipse集成 ...
- IDEA部署 java Web项目 常见配置
前言 从eclipse上转到idea上,第一次使用idea部署web项目,真折磨人,写了一个 helloworld 5分钟,了解idea部署web项目5小时. 我使用的是idea 2019.1版本,其 ...
- Eclipse中导入Maven Web项目并配置其在Tomcat中运行
今天因为实习的关系需要讲公司已经开发的项目导入进Eclipse,而公司的项目是用Maven来构建的所以,需要将Maven项目导入进Eclipse下. 自己因为没有什么经验所以搞了得两个多小时,在这里和 ...
- 转载 Log4j2在WEB项目中配置
最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...
随机推荐
- docker desktop 与 wmware tv-x
开启WSL2,获得docker desktop的最佳性能 windows默认拥有WSL1 PS C:\Users\supermao> wsl --list --verbose NAME STAT ...
- 使用numpy计算分子内坐标
技术背景 当我们打开一个用于表示分子构象的xyz文件或者pdb文件,很容易可以理解这种基于笛卡尔坐标的空间表征方法.但是除了笛卡尔坐标表示方法之外,其实也有很多其他的方法用于粗粒化或者其他目的的表征方 ...
- Qt6使用SeriaPortl包
简介: 最近使用Qt6.0开发一个自己串口小工具的时候,遇到了没有QtSerialPort包的情况,一番折腾终于找到了解决方案... 一. 在系统自带的卸载更改程序中,找到Qt,点击卸载 二. 点击添 ...
- 全同态(Fully Homomorphic Encryption, FHE)和半同态(Partially Homomorphic Encryption, PHE)介绍
全同态(Fully Homomorphic Encryption, FHE)和半同态(Partially Homomorphic Encryption, PHE) 全同态加密(FHE)是指一种加密方案 ...
- 软件测试从小白进阶高手-Python自动化+Jmeter性能+App项目+接口测试
软件测试从小白进阶高手-Python自动化+Jmeter性能+App项目+接口测试 软件测试技能,包括Python自动化.Jmeter性能测试.App项目测试.接口测试.接下来,我将从每个技能点给出一 ...
- 1. 认识IntelliJ IDEA
恐惧是本能,行动是信仰(在此感谢尚硅谷宋红康老师的教程) 1. Why IDEA ? [注]JetBrains 官方说明: 尽管我们采取了多种措施确保受访者的代表性,但结果可能会略微偏向 JetBra ...
- 记一次 .NET 某企业采购平台 崩溃分析
一:背景 1. 讲故事 前段时间有个朋友找到我,说他们的程序有偶发崩溃的情况,让我帮忙看下怎么回事,针对这种 crash 的程序,用 AEDebug 的方式抓取一个便知,有了 dump 之后接下来就可 ...
- Python运维开发之路《函数进阶》
面向对象类的进阶 抽象类 python 没有抽象类.接口的概念,所以要实现这种功能需要导入abc模块 py2:导入abc函数,_metaclass__ = abc.ABCMeta;在强制调用类下:@a ...
- 详解Django请求与响应:深入理解Web Http交互的核心机制
本文深入探讨了 Django 中的请求与响应处理,从 Django 请求和响应的基础知识.生命周期,到 HttpRequest 和 HttpResponse 对象的详细介绍.同时,讨论了 Django ...
- 如何将Maven项目快速改造成一个java web项目(方式二)
原始的maven项目,使用IDEA打开后,目录结构如下所示 删除pom.xml文件,删除resource目录,将java目录下的代码放到项目根目录下, 将webapp目录放到项目根目录下.如下图所示 ...