首页 > 其他分享 >Vue3+TypeScript 项目中,配置 ESLint 和 Prettier

Vue3+TypeScript 项目中,配置 ESLint 和 Prettier

时间:2023-01-30 14:35:12浏览次数:55  
标签:vue off prettier no typescript TypeScript Prettier Vue3 eslint

接上篇:从0搭建vite-vue3-ts项目框架:配置less+svg+pinia+vant+axios

文档同步项目gitee:https://gitee.com/lixin_ajax/vue3-vite-ts-pinia-vant-less.git

 

一、Eslint:用于检测代码

安装eslint相关依赖

yarn add eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin -D

eslint-plugin-vue:仅支持vue,提供的规则可以支持 .vue\js\jsx\ts\tsx 文件校验

@typescript-eslint/parser:解析器,让ESLint拥有规范TypeScript代码的能力

@typescript-eslint/eslint-plugin:插件,包含一系列TypeScript的ESint规则

 

初始化eslint

npx eslint --init

选择项目eslint配置,回车确认,空格多选

√ How would you like to use ESLint? · style    你希望怎样使用eslint
√ What type of modules does your project use? · esm  你的项目使用什么模块
√ Which framework does your project use? · vue 项目框架
√ Does your project use TypeScript? · No / Yes  是否使用typescript
√ 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-with-typescript  项目风格
√ What format do you want your config file to be in? · JavaScript  配置文件格式
√ Would you like to install them now? · No / Yes  确认是否安装
√ Which package manager do you want to use? · yarn 安装方式

安装完成,项目根目录生成.eslintrc.cjs文件

// .eslintrc.cjs

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'plugin:vue/vue3-essential',
    'standard-with-typescript'
  ],
  overrides: [
  ],
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: [
    'vue'
  ],
  rules: {
  }
}

在.eslintrc.cjs rules中配置eslint规则细节

rules配置eslint官网:https://eslint.org/docs/latest/rules/ 

腾讯云开发社区中文文档:https://cloud.tencent.com/developer/doc/1078

常用规则,参考:https://blog.csdn.net/ivenqin/article/details/104673237/

eslint-plugin-vue rules:https://eslint.vuejs.org/rules/

我的rules:

rules: {
    "vue/no-v-html": "off",
    "vue/script-setup-uses-vars": "off",
    "@typescript-eslint/ban-ts-ignore": "off",
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-var-requires": "off",
    "@typescript-eslint/no-empty-function": "off",
    "vue/custom-event-name-casing": "off",
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": "off",
    "@typescript-eslint/ban-ts-comment": "off",
    "@typescript-eslint/ban-types": "off",
    "@typescript-eslint/no-non-null-assertion": "off",
    "@typescript-eslint/explicit-module-boundary-types": "off",
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        argsIgnorePattern: "^_",
        varsIgnorePattern: "^_",
      },
    ],
    "no-unused-vars": [
      "error",
      {
        argsIgnorePattern: "^_",
        varsIgnorePattern: "^_",
      },
    ],
    "space-before-function-paren": "off",

    "vue/attributes-order": "off",
    "vue/one-component-per-file": "off",
    "vue/html-closing-bracket-newline": "off",
    "vue/max-attributes-per-line": "off",
    "vue/multiline-html-element-content-newline": "off",
    "vue/singleline-html-element-content-newline": "off",
    "vue/attribute-hyphenation": "off",
    "vue/require-default-prop": "off",
    "vue/require-explicit-emits": "off",
    "vue/html-self-closing": [
      "error",
      {
        html: {
          void: "always",
          normal: "never",
          component: "always",
        },
        svg: "always",
        math: "always",
      },
    ],
    "vue/multi-word-component-names": "off",
    "vue/no-parsing-error": ["off"],
    "eol-last": "off",
  },

  

 

二、Prettier:用于格式化代码

 安装prettier相关依赖

yarn add prettier eslint-config-prettier eslint-plugin-prettier stylelint-config-prettier -D

eslint-config-prettier:解决eslint和prettier冲突

eslint-config-prettier:将prettier作为eslint规则

stylelint-config-prettier:关闭所有不必要的或者有可能与Prettier冲突的规则

 

修改.eslintrc.cjs,配置prettier

  extends: [
    "plugin:prettier/recommended",
  ],
  plugins: ["prettier"],
  rules: {
    "prettier/prettier": [
      "error",
      {
        endOfLine: "auto",
      },
    ],
  }

  

修改package.json,配置修复项目格式命令

"scripts": {
    "lint-fix": "eslint --fix --ext .js --ext .jsx --ext .ts --ext .d.ts --ext .vue src/"
  },

或直接

"scripts": {
    "lint-fix": "eslint . --fix"
  },

执行命令,yarn lint-fix,项目按照eslint-prettier规则进行代码格式化

配置完成!

标签:vue,off,prettier,no,typescript,TypeScript,Prettier,Vue3,eslint
From: https://www.cnblogs.com/jing-zhe/p/16934493.html

相关文章

  • Asp.Net7 与 Vue3 组成的 BFF模式
    大家好,我是没有好工作的后端开发工程师,兼云原生方向的运维,被社会毒打被迫学习前端,写文保平安。介绍BFF模式BackendForFrontend(服务于前端的后端)初看我们会感觉它是M......
  • TypeScript的super
    (function(){classAnimal{name:string;constructor(name:string){this.name=name;}sayHello(){console.log('动物再叫---');}}cl......
  • Vue3学习(二) 全家桶,从 0 到 1 实战项目
    Vue3全家桶,从0到1实战项目 前端发展百花放,一技未熟百技出。未知何处去下手,关注小编胜百书。我是前端人,专注分享前端内容!本篇文章主要是,使用vite创建一个vue......
  • vue3学习(一)
    vue3的搭建项目及启动 1、npm和git提前安装好。新建文件夹右键,选择GitBashHere2、使用npm命令安装vue/cli,这里需要使用--force运行 npminstall-g@vue/cli-......
  • vue3引入ElementPlus实现OOS前端直传
    1.1开通OSS服务登录阿里云官网;将鼠标移至产品标签页,单击对象存储OSS,打开OSS产品详情页面;在OSS产品详情页,单击立即开通。1.2后端整合OSS实现文件上传在pom.xml中添......
  • vue3语法糖+ts组件传值
    在开发中有些功能是通用的,而且逻辑大致相同,像这种东西可以封成一个组件,比较常用的就是函数封装,组件封装,组件封装是需要引入到页面使用的,所以通常它会有一些自己的方法,父子......
  • [Typescript] Understanding Generics at Different Levels of Functions
    Thefollowingcodeimport{expect,it}from'vitest';import{Equal,Expect}from'../helpers/type-utils';exportinterfaceCache<T>{get:(key:string......
  • TypeScript踩坑记录
    设置experimentalDecorators无效errorTS1219:Experimentalsupportfordecoratorsisafeaturethatissubjecttochangeinafuturerelease.Setthe'experi......
  • vue3和百度地图关键字检索 定位 点击定位
    效果图在index.html中引入百度地图开放平台  去申请你的ak非常的简单可以自己百度一下<!--这个用官网给的有好多警告更具百度的把https://api.map.baidu.com......
  • GFast V3.2.1 版本发布,采用 GoFrame 2.3 + Vue3 后台管理系统
    平台简介基于全新GoFrame2.3+Vue3+ElementPlus开发的全栈前后端分离的管理系统前端采用vue-next-admin、Vue、ElementUI。特征高生产率:几分钟即可搭建一个后台管......