Vite 搭建基础脚手架
vue3-elemnt-plus-ts 项目 GitHub 地址
环境准备
创建项目
图例
本地开发
# 安装项目依赖
$ npm install
# 启动开发服务器
$ npm run dev
# 构建生产
$ npm run build
# 预览构建
$ npm run preview
依赖包管理
# 列出已安装过时的依赖
$ npm ls
# 检查已过时的的依赖
$ npm outdated
# 升级项目依赖
$ npm update
# 升级指定包依赖
$ npm update <package-name>
# 升级指定包安装最新依赖
$ npm update <package-name> --latest
项目配置
删除 .gitignore 文件以下内容
.gitignore
.vscode/*
!.vscode/extensions.json
统一编辑器配置
- 安装插件 EditorConfig for VS Code
- 在项目根目录新建 .editorconfig 文件,填入以下内容
.editorconfig
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
统一代码风格
- 安装插件 Prettier - Code formatter
- 安装依赖
$ npm install --save-dev prettier
- 在项目根目录新建 .prettierrc.json 文件,填入以下内容
.prettierrc.json
{
"semi": false,
"singleQuote": true,
"bracketSpacing": false,
"arrowParens": "avoid"
}
- 在项目根目录新建 .prettierignore 文件,填入以下内容
.prettierignore
# Ignore files:
/src/assets/fonts
/src/assets/iconfont
# Ignore all HTML files:
*.html
- 向 package.json scripts 属性中添加命令
package.json
"format": "prettier . --write --cache"
代码检查
- 安装插件 ESLint
- 安装依赖
$ npm install --save-dev eslint @eslint/js typescript-eslint
- 安装 prettier 相关依赖
$ npm install --save-dev eslint-plugin-prettier eslint-config-prettier
- 安装 vue 相关依赖
$ npm install --save-dev eslint-plugin-vue vue-eslint-parser
- 向 package.json scripts 属性中添加命令
package.json
"lint": "eslint .",
"lint:fix": "eslint . --fix"
- 在项目根目录新建 eslint.config.js 文件,填入以下内容
eslint.config.js
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
import eslintConfigPrettier from 'eslint-config-prettier'
import eslintPluginVue from 'eslint-plugin-vue'
import vueEslintParser from 'vue-eslint-parser'
export default tseslint.config(
eslint.configs.recommended,
...eslintPluginVue.configs['flat/recommended'],
eslintPluginPrettierRecommended,
eslintConfigPrettier,
{ignores: ['dist/']},
{
languageOptions: {
parser: vueEslintParser,
parserOptions: {
parser: tseslint.parser,
}
}
}
)
Git 预提交钩子
- 安装依赖 husky lint-staged
$ npm install --save-dev husky lint-staged
- 向 package.json scripts 属性中添加命令
package.json
"prepare": "husky"
- 向 package.json 添加根属性
package.json
"lint-staged": {
"*": [
"npm run lint",
"npm run format"
]
}
- 安装 Git,并设置换行符
$ git config --global core.autocrlf input
- 初始化 Git 仓库,首先需要进行第一次初始化提交
- 运行命令,生成 Git 钩子命令,使钩子生效
$ npm run prepare
- 向 .husky 目录下创建 pre-commit 文件,填入以下内容
.husky/pre-commit
$ npx lint-staged