Vite4+Typescript+Vue3+Pinia 从零搭建(4) - 代码规范
项目代码同步至码云 weiz-vue3-template
要求代码规范,主要是为了提高多人协同和代码维护效率,结合到此项目,具体工作就是为项目配置eslint和prettier。
editorconfig
安装 EditorConfig for VS Code 插件,根目录下新建 .editorconfig 文件,增加以下配置
[*.{js,jsx,ts,tsx,vue}]
indent_style = space
indent_size = 2
end_of_line = crlf
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 120
如果是非windows系统,end_of_line 设置为 cr
eslint
安装可以参考官方教程 vue.js代码规范。
在这里,我们建议使用另一种方式,安装后,通过命令初始化。
1. 安装
npm i eslint -D
2. 初始化
npm init @eslint/config
以下是操作实例:
PS D:\workspace\vue3\weiz-vue3-template> npm init @eslint/config
Need to install the following packages:
@eslint/create-config@0.4.6
Ok to proceed? (y)
# 输入y开始安装
? How would you like to use ESLint? ... # 如何使用eslint
To check syntax only
To check syntax and find problems
> To check syntax, find problems, and enforce code style # 检查语法、发现问题并强制执行代码风格
# 选择第三项
? What type of modules does your project use? ... # 项目使用哪种类型的模块
> JavaScript modules (import/export)
CommonJS (require/exports)
None of these
# 选择第一项
? Which framework does your project use? ... # 使用哪种框架
React
> Vue.js
None of these
# 选择 vue
? Does your project use TypeScript? » No / Yes # 项目里是否使用了ts
# 选择yes
? Where does your code run? ... (Press <space> to select, <a> to toggle all, <i> to invert selection)
# 代码运行环境,输入空格选择,可以多选
√ Browser
√ Node
# 都选中后回车
Use a popular style guide # 使用一种流行的风格指南
> Answer questions about your style # 自定义你的style
# 选择第二项
? What format do you want your config file to be in? ... # 你的config配置文件类型
> JavaScript
YAML
JSON
# 建议选择js
? What style of indentation do you use? ... # 使用什么样的缩进风格
Tabs
> Spaces
# 建议空格
? What quotes do you use for strings? ... # 字符串使用单引号还是双引号
Double
> Single
# 单引号
? What line endings do you use? ... # 行尾格式
Unix
> Windows
# Windows,如果是非windows系统,选择 unix
? Do you require semicolons? » No / Yes # 是否需要分号
# No
@typescript-eslint/eslint-plugin@latest eslint-plugin-vue@latest @typescript-eslint/parser@latest eslint@latest
? Would you like to install them now? » No / Yes
# 检查后列出以上项目,选择yes安装
? Which package manager do you want to use? ... # 使用哪种安装方式
> npm
yarn
pnpm
# 选择npm,然后等待安装完成
...
added 138 packages, changed 1 package, and audited 184 packages in 50s
39 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
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,如果我们熟悉的话,后续就可以直接安装
npm i @typescript-eslint/eslint-plugin@latest eslint-plugin-vue@latest @typescript-eslint/parser@latest eslint@latest -D
然后在根目录下新建 .eslintrc.cjs,然后把我们常用的配置复制进去即可。
4. .eslintrc.cjs 配置
以下附带基础配置:
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:vue/vue3-essential',
'plugin:prettier/recommended' // 后续兼容prettier
],
overrides: [
{
env: {
node: true
},
files: ['.eslintrc.{js,cjs}'],
parserOptions: {
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest',
parser: '@typescript-eslint/parser',
sourceType: 'module'
},
plugins: ['@typescript-eslint', 'vue'],
rules: {
indent: ['error', 2],
'linebreak-style': ['error', 'windows'],
quotes: ['error', 'single'],
semi: ['error', 'never']
}
}
5. .eslintignore
根目录下新建 .eslintignore 文件,增加需要忽略类型检查的文件和目录
node_modules
dist
public
*.md
*.txt
.vscode
index.html
6. 增加命令
修改 package.json
"scripts": {
"lint": "eslint --fix --ext .ts,.tsx,.vue,.js,.jsx --max-warnings 0"
}
运行 `npm run lint`,即可修复相关代码问题
prettier
prettier 是为了方便格式化代码,它的安装比较简单,后两个依赖是为了解决和 eslint 的冲突
1. 安装
npm i prettier eslint-config-prettier eslint-plugin-prettier -D
2. .prettierrc
根目录下新建 .prettierrc 文件,并增加以下配置
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 120,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"semi": false,
"endOfLine": "crlf"
}
如果是非windows,endOfLine 设置为 cr
3. .prettierignore
根目录下新建 .prettierignore 文件,增加需要忽略格式化的文件和目录
node_modules
dist
public
*.md
*.txt
.vscode
index.html
总结
做好以上配置后,可以运行 npm run lint 修复大部分代码格式问题,或者右键使用 prettier 格式化代码,将大大提高你的编码效率。
Vite4+Typescript+Vue3+Pinia 从零搭建(4) - 代码规范的更多相关文章
- Vite-Admin后台管理系统|vite4+vue3+pinia前端后台框架实例
基于vite4.x+vue3+pinia前端后台管理系统解决方案ViteAdmin. 前段时间分享了一篇vue3自研pc端UI组件库VEPlus.这次带来最新开发的基于vite4+vue3+pinia ...
- 【vue3-element-admin 】基于 Vue3 + Vite4 + TypeScript + Element-Plus 从0到1搭建后台管理系统(前后端开源@有来开源组织)
vue3-element-admin 是基于 vue-element-admin 升级的 Vue3 + Element Plus 版本的后台管理前端解决方案,技术栈为 Vue3 + Vite4 + T ...
- 从零搭建TypeScript与React开发环境
前言 平时进行开发大多数是基于vue-cli或者create-react-app等官方或者公司内部搭建的脚手架. 我们业务仔做的最多就是npm i和npm run dev或者npm start,然 ...
- 从零搭建基于webpack的Electron-Vue3项目(1)——基于webpack的Vue3项目搭建
从零搭建基于webpack的Electron-Vue3项目(1)--基于webpack的Vue3项目搭建 前言 本篇文章内容,主要是基于webpack的Vue3项目开发环境进行搭建,暂时还不涉及到El ...
- 从零搭建react hooks项目(github有源代码)
前言 首先这是一个react17的项目,包含项目中常用的路由.状态管理.less及全局变量配置.UI等等一系列的功能,开箱即用,是为了以后启动项目方便,特地做的基础框架,在这里分享出来. 这里写一下背 ...
- 从零搭建基于golang的个人博客网站
原文链接 : http://www.bugclosed.com/post/14 从零搭建个人博客网站需要包括云服务器(虚拟主机),域名,程序环境,博客程序等方面.本博客 就是通过这几个环节建立起来的, ...
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...
- 从零搭建docker+jenkins 自动化部署环境
从零搭建docker+jenkins+node.js自动化部署环境 本次案例基于CentOS 7系统 适合有一定docker使用经验的人阅读 适合有一定linux命令使用经验的人阅读 1.docker ...
- TypeScript完全解读(26课时)_1.TypeScript完全解读-开发环境搭建
1.TypeScript完全解读-开发环境搭建 初始化项目 手动创建文件夹 D:\MyDemos\tsDemo\client-demo 用VSCode打开 npm init:初始化项目 然后我们的项目 ...
- 从零搭建consul
从零搭建consul 原文链接:https://blog.csdn.net/weixin_42107541/article/details/87640807#2linux_25 从零搭建consul1 ...
随机推荐
- WPF实现类似ChatGPT的逐字打印效果
背景 前一段时间ChatGPT类的应用十分火爆,这类应用在回答用户的问题时逐字打印输出,像极了真人打字回复消息.出于对这个效果的兴趣,决定用WPF模拟这个效果. 真实的ChatGPT逐字输出效果涉及其 ...
- CSS实现文字描边效果
一.介绍最近在一个项目的宣传页中,设计师使用了文字描边效果,之前我确实没有实现过文字的描边效果,然后我在查阅资料后,知道了实现方法.文字描边分为两种:内外双描边和单外描边,也就是指在给文字加上描边效果 ...
- [ABC213E] Stronger Takahashi
2023-01-17 题目 题目传送门 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 bfs 解题思路 首先,这道题的问题是从家到鱼市摧毁障碍物的最少次数.我们很容易 ...
- vue3探索——组件通信之事件总线
Vue2.x使用EventBus进行组件通信,而Vue3.x推荐使用mitt.js. 比起Vue实例上的EventBus,mitt.js好在哪里呢?首先它足够小,仅有200bytes,其次支持全部事件 ...
- VictoriaLogs:一款超低占用的 ElasticSearch 替代方案
背景 前段时间我们想实现 Pulsar 消息的追踪流程,追踪实现的效果图如下: 实现其实比较简单,其中最重要的就是如何存储消息. 消息的读取我们是通过 Pulsar 自带的 BrokerInterce ...
- SSMS 显示行号
SSMS 显示行号 SSMS2022--工具--选项--文本编辑器--所有语言--常规--勾选"行号"--确定.
- SpringBoot进阶 CommandLineRunner 初始化配置
在Spring Boot中,CommandLineRunner接口用于在应用程序启动后执行一些特定的命令行操作.通过实现CommandLineRunner接口,您可以在Spring Boot应用程序运 ...
- 用策略模式干掉代码里大量的if-eles或则Swatch,提升B格由面向过程转为面向对象
现象 大量的分支选择型代码段看着让人头疼 for (Field field : declaredFields) { Class<?> type = field.getType(); Str ...
- pandas -- DataFrame的级联以及合并操作
博客地址:https://www.cnblogs.com/zylyehuo/ 开发环境 anaconda 集成环境:集成好了数据分析和机器学习中所需要的全部环境 安装目录不可以有中文和特殊符号 jup ...
- junit4单元测试报错Invalid project specified
junit4单元测试报错Invalid project specified. 前天在进行单元测试的时候出现了Invalid project specified的报错查了一下发现是项目名字的问题.项目名 ...