当前位置:   article > 正文

vue3+vite+TS配置EsLint_vue3 vite ts eslint 配置

vue3 vite ts eslint 配置

当你搭建好vue3+vite+TS模板后,想引入eslint规范

第一步

1.安装EsLint

 npm i -D eslint

2.初始化配置EsLint

npx eslint --init

 1.选择模式: To check syntax, find problems, and enforce code style 严格模式

  1. You can also run this command directly using 'npm init @eslint/config'.
  2. ? How would you like to use ESLint? ...
  3. To check syntax only
  4. To check syntax and find problems
  5. > To check syntax, find problems, and enforce code style

2.选择语言模块:选择javascript

  1. ? What type of modules does your project use? ...
  2. > JavaScript modules (import/export)
  3. CommonJS (require/exports)
  4. None of these

3.选择语言框架 选择vue.js

  1. ? Which framework does your project use? ...
  2. React
  3. > Vue.js
  4. None of these

4.是否使用ts,视自己情况而定,我选择的Yes

 ? Does your project use TypeScript? » No / Yes

5.代码在哪里运行 使用空格键全选 浏览器+node

  1. ? Where does your code run? ... (Press <space> to select, <a> to toggle all, <i> to invert selection)
  2. √ Browser
  3. √ Node

6.选择一个风格:选择流行的即可

  1. ? How would you like to define a style for your project? ...
  2. > Use a popular style guide
  3. Answer questions about your style

7.你想遵循哪一种风格指南? 选择 Standard 我一直用的这个社区指南,感觉很好。认可度也高

  1. ? Which style guide do you want to follow? ...
  2. Airbnb: https://github.com/airbnb/javascript
  3. > Standard: https://github.com/standard/standard
  4. Google: https://github.com/google/eslint-config-google
  5. XO: https://github.com/xojs/eslint-config-xo

8.你希望您的配置文件是什么格式? 选择js即可

  1. ? What format do you want your config file to be in? ...
  2. > JavaScript
  3. YAML
  4. JSON

9.可能会出现下面的提示,选择yes即可

  1. ? The style guide "standard" requires eslint@^7.12.1. You are currently using eslint@8.10.0.
  2. Do you want to downgrade? » No / Yes

10.会问你是用npm安装还是yarn安装,视自己情况而定,我这里选择的npm(yes)

? Would you like to install them now with npm? » No / Yes

3.安装完成之后,在项目根目录会出现.eslintrc.js文件

  1. // .eslintrc.js 文件
  2. module.exports = {
  3. env: {
  4. browser: true,
  5. es2021: true,
  6. node: true
  7. },
  8. extends: [
  9. 'plugin:vue/essential',
  10. 'standard'
  11. ],
  12. parserOptions: {
  13. ecmaVersion: 'latest',
  14. sourceType: 'module'
  15. },
  16. plugins: [
  17. 'vue'
  18. ],
  19. rules: {
  20. }
  21. }

5. 继续安装 

  1. npm i -D vite-plugin-eslint
  2. npm i @typescript-eslint/parser //一个eslint的解析器,允许使用typescrpt ESTree来对ts代码进行规范约束。
  3. npm i @typescript-eslint/eslint-plugin //一个为ts代码提供约束规则的插件,需要确保项目中已安装

6. 在package.json文件中添加: "@babel/eslint-parser": "^7.17.0"(我忘了npm的安装名了~)

  1. // package.json 文件
  2. "devDependencies": {
  3. ...
  4. "@babel/eslint-parser": "^7.17.0"
  5. }

7. 执行npm install

npm install

8. 配置vite.config.js文件

  1. // vite.config.js 文件
  2. import eslintPlugin from 'vite-plugin-eslint'
  3. export default defineConfig({
  4. plugins: [
  5. vue(),
  6. // 添加下面这块
  7. eslintPlugin({
  8. include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
  9. })
  10. ]
  11. }

9. 配置.eslintrc.js文件

  1. module.exports = {
  2. env: {
  3. browser: true,
  4. es2021: true,
  5. 'vue/setup-compiler-macros': true
  6. },
  7. extends: [
  8. 'plugin:vue/vue3-essential',
  9. 'standard-with-typescript',
  10. 'plugin:@typescript-eslint/recommended'
  11. ],
  12. overrides: [
  13. ],
  14. parser: 'vue-eslint-parser',
  15. parserOptions: {
  16. ecmaVersion: 'latest',
  17. sourceType: 'module',
  18. parser: '@typescript-eslint/parser'
  19. },
  20. plugins: [
  21. 'vue',
  22. '@typescript-eslint'
  23. ],
  24. rules: {
  25. semi: [2, 'never'], // 禁止尾部使用分号“ ; ”
  26. 'no-var': 'error', // 禁止使用 var
  27. indent: ['error', 2], // 缩进2
  28. 'no-mixed-spaces-and-tabs': 'error', // 不能空格与tab混用
  29. quotes: [2, 'single'] // 使用单引号
  30. }
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/769944
推荐阅读
相关标签
  

闽ICP备14008679号