一文学会搭建vue3+TypeScript项目规范

「这是我参与 2022 首次更文挑战的第 1 天,活动详情查看:2022 首次更文挑战」。

一、前言

前端攻城狮之必会技能 -- 搭建前端项目的开发规范

先说下文章的背景,以下所有的配置均在 @vue/cli 创建的vue3 + TypeScript 项目中配置;

没有安装@vue/cli的看这里@vue/cli v4.x.x 使用指南

创建项目时的选择项(即在终端执行命令vue create myapp)如下:

  • 选择自定义配置选项 image.png

  • 选择一些配置 image.png

  • 选择vue.js版本 image.png

  • 选择 eslint image.png

  • 选择其他的一些配置 image.png

阅读本文,你将学到:

1. 学会在项目中集成`editorconfig`2. 学会在项目中集成`prettier`;
3. 学会在项目中集成`eslint`代码检测工具;
4. 学会在项目中使用`git husky`5. 学会如何使用`git commit` 规范;
复制代码

二、代码规范

2.1 集成 editorconfig 配置

EditorConfig 有助于为不同 IDE 编辑器处理同一项目的不同开发人员维护一致的编码风格。 在项目中新建.editorconfig文件,写入如下配置:

# http://editconfig.org

root = true

[*] # 表示所有文件适用
charset = utf-8 # 设置文件字符集为 utf-8
indent_style = tab # 缩进风格(tab | space)
indent_size = 2 # 缩进大小
end_of_line = lf # 控制换行类型(lf | cr | crlf)
trim_trailing_whitespace = true # 去除行首的任意空白字符
insert_final_newline = true # 始终在文件末尾插入一个新行

[*.md] # 表示仅md文件适用以下规则
max_line_length = off
trim_trailing_whitespace = false

复制代码

VSCode 需要安装一个插件:EditorConfig for VS Code

image.png

2.2 集成 prettier 工具

Prettier 是一款强大的代码格式化工具,支持 javascript、TypeScript、CSS、SCSS、Less、JSX、vue、Markdown 等语言,基本上前端能用的文件格式都可以使用 Prettier 格式化,是当下最流行的代码格式化工具。

2.2.1. 安装prettier

npm install --save-dev --save-exact prettier
复制代码

2.2.2. 配置.prettierrc文件

在项目根目录创建.prettierrc文件,并配置以下内容:

  • useTabs: 使用空格还是 tab 缩进;
  • tabWidth:缩进的大小;
  • printWidth:当行字符的长度;
  • singleQuote:使用单引号还是双引号;
  • trailingComma:在多行输入的尾逗号是否添加;
  • semi:句末是否要加分号;
{
	"useTabs": false,
	"tabWidth": 2,
	"printWidth": 80,
	"singleQuote": true,
	"trailingComma": "none",
	"semi": false
}
复制代码

2.2.3. 配置.prettierignore文件

配置不被prettier格式化的文件:

/dist/*
/node_modules/**
/public/**
.local
.output.js

**/*.svg
**/*.sh
复制代码

2.2.4. 安装 vscode 插件Prettier - Code formatter

image.png 这个时候保存代码,应该会按照.prettierrc配置的规则生效;如果没有生效,还需要修改 vscode 编辑器的默认格式化代码工具为 prettier,通过 VSCode -> 首选项 -> 设置(settings.json),可配置 VSCode 编辑器的默认格式化工具,也可根据语言设置其对应的默认格式化工具:

{
	"editor.formatOnSave": true,
	"editor.defaultFormatter": "esbenp.prettier-vscode", // 设置编辑器的默认格式化工具为 prettier
	"[vue]": {
		// 根据语言设置其对应的默认格式化工具
		"editor.defaultFormatter": "esbenp.prettier-vscode", // 设置 javascript 的默认格式化工具为 prettier
		"editor.formatOnSave": true // 保存的时候自动格式化
	}
}
复制代码

2.2.5. 测试prettier

  • 测试 1:在代码中保存代码;
  • 测试 2:配置一次性修改的命令;

在 package.json 中配置一个 scripts(格式化项目中全部文件):

    "prettier": "prettier --write ."
复制代码

2.3 集成 Eslint 代码检测工具

2.3.1 Eslint 环境

在前面创建项目的时候,我们就选择了 ESLint,所以 Vue 会默认帮助我们配置需要的 ESLint 环境;

2.3.2、下载 vscode 插件 ESLint插件

image.png

2.3.3 解决 eslint 和 prettier 的冲突

安装插件:(vue 在创建项目时,如果选择 prettier,那么这两个插件会自动安装)

npm i eslint-plugin-prettier eslint-config-prettier -D
复制代码

修改.eslintrc.js文件内容,在extends数组中新增一项:

module.exports = {
	...
	extends: [
		...
		'plugin:prettier/recommended' // 解决prettier和eslint的冲突
	],
}
复制代码

2.4 git Husky 和 eslint

2.4.1 git commit 时 eslint 自动修复

虽然我们已经要求项目使用 eslint 了,但是不能保证组员提交代码之前都将 eslint 中的问题解决掉了:

  • 也就是我们希望保证代码仓库中的代码都是符合 eslint 规范的;

  • 那么我们需要在组员执行 git commit 命令的时候对其进行校验,如果不符合 eslint 规范,那么自动通过规范进行修复;

**那么如何做到这一点呢?**可以通过 Husky 工具:

  • husky 是一个 git hook 工具,可以帮助我们触发 git 提交的各个阶段:pre-commitcommit-msgpre-push

如何使用 husky 呢?

这里我们可以使用自动配置命令:

npx husky-init && npm install
复制代码

这里会做三件事:

1.安装 husky 相关的依赖:

image-20210723112648927

2.在项目目录下创建 .husky 文件夹:

npx huksy install
复制代码

image-20210723112719634

3.在 package.json 中添加一个脚本:

image-20210723112817691

接下来,我们需要去完成一个操作:在进行 commit 时,执行 lint 脚本:

image-20210723112932943

这个时候我们执行 git commit 的时候会自动对代码进行 lint 校验。

2.4.2 git commit 规范

通常我们的 git commit 会按照统一的风格来提交,这样可以快速定位每次提交的内容,方便之后对版本进行控制。

但是如果每次手动来编写这些是比较麻烦的事情,我们可以使用一个工具:Commitizen

  • Commitizen 是一个帮助我们编写规范 commit message 的工具;
  1. 安装 Commitizen
npm install commitizen -D
复制代码
  1. 安装 cz-conventional-changelog,并且初始化 cz-conventional-changelog
npx commitizen init cz-conventional-changelog --save-dev --save-exact
复制代码

这个命令会帮助我们安装 cz-conventional-changelog

image-20210723145249096

并且在 package.json 中进行配置:

这个时候我们提交代码需要使用 npx cz

  • 第一步是选择 type,本次更新的类型
Type 作用
feat 新增特性 (feature)
fix 修复 Bug(bug fix)
docs 修改文档 (documentation)
style 代码格式修改(white-space, formatting, missing semi colons, etc)
refactor 代码重构(refactor)
perf 改善性能(A code change that improves performance)
test 测试(when adding missing tests)
build 变更项目构建或外部依赖(例如 scopes: webpack、gulp、npm 等)
ci 更改持续集成软件的配置文件和 package 中的 scripts 命令,例如 scopes: Travis, Circle 等
chore 变更构建流程或辅助工具(比如更改测试环境)
revert 代码回退
  • 第二步选择本次修改的范围(作用域)

image-20210723150147510

  • 第三步选择提交的信息

image-20210723150204780

  • 第四步提交详细的描述信息

image-20210723150223287

  • 第五步是否是一次重大的更改

image-20210723150322122

  • 第六步是否影响某个 open issue

image-20210723150407822

我们也可以在 scripts 中构建一个命令来执行 cz:

image-20210723150526211

2.4.2 git 提交验证

如果我们按照 cz 来规范了提交风格,但是依然有同事通过 git commit 按照不规范的格式提交应该怎么办呢?

  • 我们可以通过 commitlint 来限制提交;

1.安装 @commitlint/config-conventional 和 @commitlint/cli

npm i @commitlint/config-conventional @commitlint/cli -D
复制代码

2.在根目录创建 commitlint.config.js 文件,配置 commitlint

module.exports = {
	extends: ['@commitlint/config-conventional']
};
复制代码

3.使用 husky 生成 commit-msg 文件,验证提交信息:

npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
复制代码

如果命令无效的话,可以手动在.husky 文件夹下创建 commit-msg 文件,并写入内容

image.png

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit
复制代码

三、总结

这篇文章主要讲述了如何搭建前端项目规范。在实际中,其实很多开发同学都是一直在做一些维护的项目,很难接触到项目开发规范方面的东西,文章也只是简单从几个方面来讲述了项目规范。

当然也可以看一些github上比较好的项目的项目规范,比如vuejs、react等,也会学习到不少东西。

おすすめ

転載: juejin.im/post/7054453878707716126