经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » TypeScript » 查看文章
Vite4+Typescript+Vue3+Pinia 从零搭建(4) - 代码规范
来源:cnblogs  作者:唯之为之  时间:2023/11/29 16:17:57  对本文有异议

项目代码同步至码云 weiz-vue3-template
要求代码规范,主要是为了提高多人协同和代码维护效率,结合到此项目,具体工作就是为项目配置 eslintprettier

editorconfig

安装 EditorConfig for VS Code 插件,根目录下新建 .editorconfig 文件,增加以下配置

  1. [*.{js,jsx,ts,tsx,vue}]
  2. indent_style = space
  3. indent_size = 2
  4. end_of_line = crlf
  5. trim_trailing_whitespace = true
  6. insert_final_newline = true
  7. max_line_length = 120

如果是非windows系统,end_of_line 设置为 cr

eslint

安装可以参考官方教程 vue.js代码规范
在这里,我们建议使用另一种方式,安装后,通过命令初始化。

1. 安装

  1. npm i eslint -D

2. 初始化

  1. npm init @eslint/config

以下是操作实例:

  1. PS D:\workspace\vue3\weiz-vue3-template> npm init @eslint/config
  2. Need to install the following packages:
  3. @eslint/create-config@0.4.6
  4. Ok to proceed? (y)
  5. # 输入y开始安装
  6. ? How would you like to use ESLint? ... # 如何使用eslint
  7. To check syntax only
  8. To check syntax and find problems
  9. > To check syntax, find problems, and enforce code style # 检查语法、发现问题并强制执行代码风格
  10. # 选择第三项
  11. ? What type of modules does your project use? ... # 项目使用哪种类型的模块
  12. > JavaScript modules (import/export)
  13. CommonJS (require/exports)
  14. None of these
  15. # 选择第一项
  16. ? Which framework does your project use? ... # 使用哪种框架
  17. React
  18. > Vue.js
  19. None of these
  20. # 选择 vue
  21. ? Does your project use TypeScript? ? No / Yes # 项目里是否使用了ts
  22. # 选择yes
  23. ? Where does your code run? ... (Press <space> to select, <a> to toggle all, <i> to invert selection)
  24. # 代码运行环境,输入空格选择,可以多选
  25. Browser
  26. Node
  27. # 都选中后回车
  28. Use a popular style guide # 使用一种流行的风格指南
  29. > Answer questions about your style # 自定义你的style
  30. # 选择第二项
  31. ? What format do you want your config file to be in? ... # 你的config配置文件类型
  32. > JavaScript
  33. YAML
  34. JSON
  35. # 建议选择js
  36. ? What style of indentation do you use? ... # 使用什么样的缩进风格
  37. Tabs
  38. > Spaces
  39. # 建议空格
  40. ? What quotes do you use for strings? ... # 字符串使用单引号还是双引号
  41. Double
  42. > Single
  43. # 单引号
  44. ? What line endings do you use? ... # 行尾格式
  45. Unix
  46. > Windows
  47. # Windows,如果是非windows系统,选择 unix
  48. ? Do you require semicolons? ? No / Yes # 是否需要分号
  49. # No
  50. @typescript-eslint/eslint-plugin@latest eslint-plugin-vue@latest @typescript-eslint/parser@latest eslint@latest
  51. ? Would you like to install them now? ? No / Yes
  52. # 检查后列出以上项目,选择yes安装
  53. ? Which package manager do you want to use? ... # 使用哪种安装方式
  54. > npm
  55. yarn
  56. pnpm
  57. # 选择npm,然后等待安装完成
  58. ...
  59. added 138 packages, changed 1 package, and audited 184 packages in 50s
  60. 39 packages are looking for funding
  61. run `npm fund` for details
  62. found 0 vulnerabilities
  63. Successfully created .eslintrc.cjs file in D:\workspace\vue3\weiz-vue3-template

安装完成,在根目录下生成了 .eslintrc.cjs 文件,并自带一些我们选择的配置。

3. 简易安装

通过以上安装我们发现,最终还是安装了4个依赖,@typescript-eslint/eslint-plugin@latest eslint-plugin-vue@latest @typescript-eslint/parser@latest eslint@latest,如果我们熟悉的话,后续就可以直接安装

  1. npm i @typescript-eslint/eslint-plugin@latest eslint-plugin-vue@latest @typescript-eslint/parser@latest eslint@latest -D

然后在根目录下新建 .eslintrc.cjs,然后把我们常用的配置复制进去即可。

4. .eslintrc.cjs 配置

以下附带基础配置:

  1. module.exports = {
  2. env: {
  3. browser: true,
  4. es2021: true,
  5. node: true
  6. },
  7. extends: [
  8. 'eslint:recommended',
  9. 'plugin:@typescript-eslint/recommended',
  10. 'plugin:vue/vue3-essential',
  11. 'plugin:prettier/recommended' // 后续兼容prettier
  12. ],
  13. overrides: [
  14. {
  15. env: {
  16. node: true
  17. },
  18. files: ['.eslintrc.{js,cjs}'],
  19. parserOptions: {
  20. sourceType: 'script'
  21. }
  22. }
  23. ],
  24. parserOptions: {
  25. ecmaVersion: 'latest',
  26. parser: '@typescript-eslint/parser',
  27. sourceType: 'module'
  28. },
  29. plugins: ['@typescript-eslint', 'vue'],
  30. rules: {
  31. indent: ['error', 2],
  32. 'linebreak-style': ['error', 'windows'],
  33. quotes: ['error', 'single'],
  34. semi: ['error', 'never']
  35. }
  36. }

5. .eslintignore

根目录下新建 .eslintignore 文件,增加需要忽略类型检查的文件和目录

  1. node_modules
  2. dist
  3. public
  4. *.md
  5. *.txt
  6. .vscode
  7. index.html

6. 增加命令

修改 package.json

  1. "scripts": {
  2. "lint": "eslint --fix --ext .ts,.tsx,.vue,.js,.jsx --max-warnings 0"
  3. }
  4. 运行 `npm run lint`,即可修复相关代码问题

prettier

prettier 是为了方便格式化代码,它的安装比较简单,后两个依赖是为了解决和 eslint 的冲突

1. 安装

  1. npm i prettier eslint-config-prettier eslint-plugin-prettier -D

2. .prettierrc

根目录下新建 .prettierrc 文件,并增加以下配置

  1. {
  2. "useTabs": false,
  3. "tabWidth": 2,
  4. "printWidth": 120,
  5. "singleQuote": true,
  6. "trailingComma": "none",
  7. "bracketSpacing": true,
  8. "semi": false,
  9. "endOfLine": "crlf"
  10. }

如果是非windows,endOfLine 设置为 cr

3. .prettierignore

根目录下新建 .prettierignore 文件,增加需要忽略格式化的文件和目录

  1. node_modules
  2. dist
  3. public
  4. *.md
  5. *.txt
  6. .vscode
  7. index.html

总结

做好以上配置后,可以运行 npm run lint 修复大部分代码格式问题,或者右键使用 prettier 格式化代码,将大大提高你的编码效率。

原文链接:https://www.cnblogs.com/weizwz/p/17862355.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号