首页 > 其他分享 >给nuxt3添加 ESLint 和 Prettier

给nuxt3添加 ESLint 和 Prettier

时间:2024-01-14 20:15:38浏览次数:50  
标签:lint nuxt3 -- eslint yarn prettier ESLint true Prettier

前言

在这篇文章中,我们将介绍ESLint和Prettier,用于在Nuxt 3项目中自动设置代码样式格式。

配置自动设置代码样式有这些好处:

  1. 一致性:自动执行某种风格。特别是在代码审查中避免吹毛求疵的论点。
  2. 节省时间:无需手动格式化代码。
  3. 代码质量:捕捉代码中的潜在问题,例如语法错误和编码风格冲突。

正文

安装依赖

# ESLint
yarn add --dev eslint

# Prettier
yarn add --dev prettier eslint-config-prettier eslint-plugin-prettier

# TypeScript support
yarn add --dev typescript @typescript-eslint/parser @nuxtjs/eslint-config-typescript

配置

将以下规则添加到ESLint配置(.eslintrc.cjs):

// .eslintrc.cjs

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
  },
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
  },
  extends: ['@nuxtjs/eslint-config-typescript', 'plugin:prettier/recommended'],
  plugins: [],
  rules: {},
}

将以下规则添加到Prettier配置(.prettierrc.cjs):

module.exports = {
  /*打印宽度,超过后,会将属性换行*/
  printWidth: 120,
  /*禁止使用尾随逗号,对象和数组最后一个逗号去掉*/
  trailingComma: "none",
  /*在对象字面量中的括号之间添加空格*/
  bracketSpacing: true,
  /*使用单引号而不是双引号来定义字符串*/
  singleQuote: true,
  /*当箭头函数只有一个参数时,省略参数前后的括号*/
  arrowParens: "avoid",
  /*script和style标签中间的内容缩进*/
  vueIndentScriptAndStyle: true,
  // 将>多行 HTML(HTML、JSX、Vue、Angular)元素放在最后一行的末尾,而不是单独放在下一行(不适用于自闭合元素
  bracketSameLine:false
};

添加脚步命令

// package.json

{
  // ...
  "scripts": {
    // ...
    "lint:js": "eslint --ext \".ts,.vue\" --ignore-path .gitignore .",
    "lint:prettier": "prettier --check .",
    "lint": "yarn lint:js && yarn lint:prettier",
    "lintfix": "prettier --write --list-different . && yarn lint:js --fix"
    // ...
  }
  // ...
}

补充

这里强烈建议给项目增加一个 .editorconfig 文件,可以约束代码编辑器的基本规则

// .editorconfig  文件
# http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

参考:https://dev.to/tao/adding-eslint-and-prettier-to-nuxt-3-2023-5bg
以和为贵!让 ESlint、Prettier 和 EditorConfig 互不冲突

标签:lint,nuxt3,--,eslint,yarn,prettier,ESLint,true,Prettier
From: https://www.cnblogs.com/shuiche/p/17964098

相关文章

  • eslintrc.js配置文件详解
    转自:https://www.jianshu.com/p/fe727ad2bbf4通俗说就是统一代码规范。配置参数rules:{"规则名":[规则值,规则配置]}规则值"off"或者0//关闭规则关闭"warn"或者1//在打开的规则作为警告(不影响退出代码)"error"或者2//把规则作为一个错误(退出代码触......
  • Nuxt3 基础总结
    前言Nuxt3的对比之前的2和1,只能感叹前端发展的越来越快了,不学无术开发更快打包更小支持vite支持vue3支持自动引入支持文件路由支持布局系统支持多种渲染模式支持typescript支持composition-api 安装NUXT3需要node大于16的版本brew更新node  bre......
  • React项目中报错:Parsing error: The keyword 'import' is reservedeslint
    记得更改完配置后,要重启编辑器(如:VSCode)!!!记得更改完配置后,要重启编辑器(如:VSCode)!!!记得更改完配置后,要重启编辑器(如:VSCode)!!!这个错误通常发生在你尝试在一个不支持ES6模块语法的环境中使用import关键字。ESLint默认使用的是ES5语法,如果你想使用ES6或者更新的语法,你......
  • Git — husky + eslint 实现提交前校验与规范提交
    node版本:=14.21.3||>16.0.0一、配置ESlint1.1安装eslintnpminstalleslint-D1.2初始化eslint,生成配置文件npxeslint--init1.3在编辑器安装ESlint插件1.4通过执行命令检测文件代码规范#./src为需要检测的文件路径npxeslint./src二、配置husky2.1安......
  • Teamcenter AWC开发报错 ESLintError in plugin "gulp-eslint"
    1、npmrunrefresh没有报错,npmrunbuild会报错✖34problems(1error,33warnings)0errorsand12warningspotentiallyfixablewiththe`--fix`option.[08:47:54]'audit'erroredafter6.31s[08:47:54]ESLintErrorinplugin"gulp-eslint&quo......
  • vscode自动保存设置为afterDelay后prettier不生效
    "files.autoSave":"afterDelay""editor.formatOnSave":true这两个配置不能一起使用解决方法:1.将"afterDelay"修改为“onFocusChange"2.设置成onFocusChange或者onWindowChange3.关闭重启vscode 参考文件:files.autoSaveandeditor.formatOnSave  ......
  • 解决:Expected 1 line break before closing bracket, but no line breaks found.eslin
    运行时报错以下 解决在eslintrc.jsrules下添加以下代码'vue/singleline-html-element-content-newline':'off','vue/multiline-html-element-content-newline':'off', ......
  • Eslint 的rules一些配置 (.eslintrc.js文件中的rules选项)
    rules:{//off=0,warn=1,error=2,如果是数组,第二项表示参数option//indent:[2,2],//控制缩进为2eqeqeq:1,//警告使用全等//quotes:[2,'single'],//单引号singleQuote:true,'no-console':0,//不禁用console'no-debugger......
  • 【最新最全指南】Vue项目安装eslint配置说明
    本指南都适配windows和mac下的开发项目第一步:安装1、全局安装:eslint(最好全局安装1个,配置出错debug方便)npminstalleslint-g2、项目本地dev安装:eslint(也可不安装,后面安装eslint-plugin-vue时候,默认会一起安装eslint)npminstalleslint-D3、项目本地dev安装插件:eslint-plu......
  • nuxt3构建优化
    1.性能分析network分析 Lighthouse分析利用浏览器的Lighthouse工具不仅能够了解到首屏渲染速度等信息,并还能提供相应优化建议   Bundle分析    在nuxt3不需要自行下载第三方依赖也能够进行Bundle分析 配置:在nuxt.config.ts下写入exportdefaultdefi......