Web项目如何配置Eslint

这篇具有很好参考价值的文章主要介绍了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 命令即可生效文章来源地址https://www.toymoban.com/news/detail-706913.html

"plugin:prettier/recommended", "prettier";

到了这里,关于Web项目如何配置Eslint的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue项目中ESLint配置

    ESLint最初是由Nicholas C. Zakas 于2013年6月创建的开源项目。它的目标是提供一个插件化的javascript代码检测工具。 ESLint 是一个开源的 JavaScript 代码检查工具,。代码检查是一种静态的分析,常用于寻找有问题的模式或者代码,并且不依赖于具体的编码风格。对大多数编程语言来说

    2024年02月05日
    浏览(80)
  • 【vue3+ts项目】配置eslint校验代码工具,eslint+prettier+stylelint

    package.json中 vite后面加上 --open 回答问题如下: 使用eslint仅检查语法,还是检查语法及错误,选第二个 使用的是什么模块,选第一个 项目使用的是什么框架,选vue 项目中使用TyoeScript ,选yes 项目运行在哪,选浏览器 创建的配置类型需要什么类型的,选Javascript 需要安装这些

    2024年02月09日
    浏览(75)
  • TypeScript 项目 Airbnb 语法风格 ESLint 配置

    安装: 配置: .eslintrc.js: tsconfig.json: 错误: 注意: webstorm 上修改完 tsconfig.json 的 include 属性的值,需要重启才会生效 eslint-config-airbnb-typescript Solve parserOptions.project bug with Typescript and ESLint

    2024年02月01日
    浏览(48)
  • vue3项目 - Eslint 配置代码风格

    总结: Prettier   (代码规范的插件,格式化 )--- 美观 Eslint   (规范、纠错、检验错误 )----- 纠错 首先, 禁用 Prettier 插件,安装 ESLint 插件,配置保存时自动修复 再 配置代码风格文件 .eslintrc.cjs (Prettier 配置 + ESLint 配置, 附配置代码 ) 配置内容 如, Prettier:单引号、

    2024年02月04日
    浏览(35)
  • vue项目中配置eslint和prettier

    eslint检查语法错误,格式问题并不重要 prettier是格式化工具,保证代码美观 vscode插件Eslint(务必安装),错误标红,保存的时候自动修正eslint错误 如果项目中一开始就没有配置,用下面的方法从零配置 若项目中已经有别人配好的,可根据需要修改规则 eslint插件,初始化,生

    2024年02月07日
    浏览(59)
  • Vue typescript项目配置eslint+prettier

    前言 本文基于 “vite”: “^5.0.0” 1.安装依赖 安装 eslint 安装 eslint-plugin-vue 主要用于检查 Vue 文件语法 安装 prettier 及相关插件 安装 typescript 解析器、规则补充 2.根目录创建 .eslintrc.cjs 3.根目录创建 .prettierrc.cjs 4.配置 package.json 的 scripts 字段 5.测试配置 如果本篇文章对你有帮

    2024年01月16日
    浏览(43)
  • 如何关闭vue项目中的[eslint]校验

    要关闭Vue项目中的ESLint校验,可以按照以下步骤进行操作: 打开项目根目录下的 .eslintrc.js 文件(如果没有该文件,则创建一个新的)。 在文件中添加以下代码: 保存文件并关闭。 重新启动Vue项目,ESLint校验应该已经关闭了。 通过将 \\\'no-unused-vars\\\' 规则设置为 \\\'off\\\' ,你可以

    2024年02月04日
    浏览(41)
  • vue项目配置eslint(附visio studio code配置)

    ? Which style guide do you want to follow? Standard ? What format do you want your config file to be in? JavaScript 编辑.eslintrc.js module.exports = { // 默认情况下,ESLint会在所有父级组件中寻找配置文件,一直到根目录。ESLint一旦发现配置文件中有 “root”: true,它就会停止在父级目录中寻找。 root: true,

    2024年04月09日
    浏览(46)
  • vue3+ts项目中eslint校验配置

    eslint中文官网:ESLint - Pluggable JavaScript linter - ESLint中文 ESLint 是一个根据方案识别并报告 ECMAScript/JavaScript 代码问题的工具,其目的是使代码风格更加一致并避免错误,合理利用可以提高代码质量。 1.安装eslint pnpm i eslint -D 2.生成eslint配置文件 npx eslint --init 3.安装vue3代码环境校

    2024年02月04日
    浏览(52)
  • 在 Vue3 项目中如何关闭 ESLint

    ESLint 是一个用于检查 JavaScript 代码质量的工具,它可以帮助开发者遵循一致的编码规范,并发现潜在的错误和问题。然而,在某些情况下,我们可能希望关闭 ESLint,例如在一些小型项目中或者在特定的开发阶段。本文将介绍如何在 Vue3 项目中关闭 ESLint。 第一步:找到配置文

    2024年02月08日
    浏览(51)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包