vue3 项目搭建
傻瓜式搭建项目,一直安装下去就完成了,git项目地址。
项目整体依赖
- Element-Plus
- Eslint
- Stylelint
- Markdownlint
- Commitlint
- Husky
需要安装
node16.0以上的版本
一、Vue3 应用创建
安装
npm create vue@latest
直接选择安装 ESLint 以及其他项目所需使用到的包
二、Element-Plus 安装
安装
npm install element-plus --save
Volar 支持
如果您使用 Volar,请在 tsconfig.json 中通过 compilerOptions.type 指定全局组件类型。
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["element-plus/global"]
}
}
按需引入
首先你需要安装 unplugin-vue-components 和 unplugin-auto-import
npm install -D unplugin-vue-components unplugin-auto-import
然后把下列代码插入到你的 Vite 或 Webpack 的配置文件中
Vite
如果需要使用 element-plus icon 的话,这个步骤可以和下一个步骤合并
// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({
// ...
plugins: [
// ...
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
})
自动导入方式使用图标
原文档:Icon 图标
安装 @iconify-json/ep 和 unplugin-icons
安装
npm install -D @iconify-json/ep unplugin-icons
修改 vite.config.ts 配置
import path from 'path'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
const pathSrc = path.resolve(__dirname, 'src')
export default defineConfig({
// ...
plugins: [
// ...
AutoImport({
// Auto import functions from Vue, e.g. ref, reactive, toRef...
// 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
imports: ['vue'],
// Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
// 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
resolvers: [
ElementPlusResolver(),
// Auto import icon components
// 自动导入图标组件
IconsResolver({
prefix: 'Icon',
}),
],
dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
}),
Components({
resolvers: [
// Auto register icon components
// 自动注册图标组件
IconsResolver({
enabledCollections: ['ep'],
}),
// Auto register Element Plus components
// 自动导入 Element Plus 组件
ElementPlusResolver(),
],
dts: path.resolve(pathSrc, 'components.d.ts'),
}),
Icons({
autoInstall: true,
}),
],
})
Icon 使用
<el-icon size="20">
<i-ep-add-location />
</el-icon>
<el-icon size="20">
<i-ep-aim />
</el-icon>
//或
<i-ep-add-location />
<i-ep-aim />
三、Eslint
创建项目时已经安装 ESLint 在这里只需要配置 .eslintrc.cjs 文件,根据需求添加或修改一些校验规则即可。
/* eslint-env node */
require('@rushstack/eslint-patch/modern-module-resolution')
module.exports = {
root: true,
'extends': [
'plugin:vue/vue3-essential',
'eslint:recommended',
'@vue/eslint-config-typescript',
'@vue/eslint-config-prettier/skip-formatting'
],
parserOptions: {
ecmaVersion: 'latest'
},
rules: {
'no-undef': 'off',
'func-names': 'off',
'linebreak-style': 'off',
'vue/no-template-shadow': 'off',
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-param-reassign': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'object-curly-newline': [
'error',
{
ExportDeclaration: { multiline: true, minProperties: 6 },
},
],
}
}
创建 .eslintignore 文件,过滤不校验文件
!/.*.js
node_modules
dist
.history
# src/components
# src/views
# src/utils
# src/assets
在 package.json 中添加以下代码
{
...
"scripts": {
...
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --ignore-path .gitignore",
"lint:fix": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
...
}
...
}
四、Stylelint
安装
npm install stylelint stylelint-config-standard stylelint-order postcss-html postcss-scss stylelint-config-standard-vue -D
创建 .stylelintrc.cjs 文件
module.exports = {
extends: [
'stylelint-config-standard',
'stylelint-config-standard-vue'
],
plugins: ['stylelint-order'],
// 不同格式的文件指定自定义语法
overrides: [
{
files: ['**/*.(scss|css|vue|html)'],
customSyntax: 'postcss-scss',
},
{
files: ['**/*.(html|vue)'],
customSyntax: 'postcss-html',
},
],
ignoreFiles: ['**/*.js', '**/*.jsx', '**/*.tsx', '**/*.ts', '**/*.json', '**/*.md', '**/*.yaml'],
rules: {
'media-feature-name-no-unknown': [true, {
ignoreMediaFeatureNames: ['min-device-pixel-ratio'],
}],
// 禁止在具有较高优先级的选择器后出现被其覆盖的较低优先级的选择器
'no-descending-specificity': null,
'selector-pseudo-element-no-unknown': [
true,
{
ignorePseudoElements: ['v-deep'],
},
],
'selector-pseudo-class-no-unknown': [
true,
{
ignorePseudoClasses: ['global', 'deep'],
},
],
// 禁用每个选择器之前插入空行
'rule-empty-line-before': null,
// 禁止小于 1 的小数有一个前导零
// 'number-leading-zero': 'never',
// 一些特殊的scss指令
'at-rule-no-unknown': [
true,
{
ignoreAtRules: ['extend', 'at-root', 'debug', 'warn', 'error', 'if', 'else', 'for', 'each', 'while', 'mixin', 'include', 'content', 'return', 'function'],
},
],
'at-rule-empty-line-before': [
'always',
{
except: ['blockless-after-same-name-blockless', 'first-nested'],
ignore: ['after-comment'],
ignoreAtRules: ['else', 'else-if'],
},
],
// 指定样式的排序
'order/properties-order': [
'position',
'top',
'right',
'bottom',
'left',
'z-index',
'display',
'justify-content',
'align-items',
'flex-shrink',
'float',
'clear',
'overflow',
'overflow-x',
'overflow-y',
'width',
'min-width',
'max-width',
'height',
'min-height',
'max-height',
'padding',
'padding-top',
'padding-right',
'padding-bottom',
'padding-left',
'margin',
'margin-top',
'margin-right',
'margin-bottom',
'margin-left',
'font-size',
'font-family',
'text-align',
'text-justify',
'text-indent',
'text-overflow',
'text-decoration',
'white-space',
'color',
'background',
'background-position',
'background-repeat',
'background-size',
'background-color',
'background-clip',
'border',
'border-style',
'border-width',
'border-color',
'border-top-style',
'border-top-width',
'border-top-color',
'border-right-style',
'border-right-width',
'border-right-color',
'border-bottom-style',
'border-bottom-width',
'border-bottom-color',
'border-left-style',
'border-left-width',
'border-left-color',
'border-radius',
'opacity',
'filter',
'list-style',
'outline',
'visibility',
'box-shadow',
'text-shadow',
'resize',
'transition',
'content',
],
},
};
创建 .stylelintignore 文件
src/static
node_modules
public
dist
src/uni_modules
在 package.json 中添加以下代码
{
...
"scripts": {
...
"lint:css": "stylelint \"./**/*.{css,scss,sass,vue,html}\"",
"lint:css:fix": "stylelint --fix \"./**/*.{css,scss,sass,vue,html}\""
...
}
...
}
五、Markdownlint
安装
npm i markdownlint-cli -D
创建 .markdownlint.json 文件
{
"line-length": false
}
在 package.json 中添加以下代码
{
...
"scripts": {
...
"lint:md": "markdownlint --ignore-path .gitignore .",
"lint:md:fix": "markdownlint --ignore-path .gitignore --fix ."
...
}
...
}
六、Commitlint
安装
npm install @commitlint/cli @commitlint/config-conventional -D
命令创建 commitlint.config.js
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
手动创建 commitlint.config.js,内容
module.exports = { extends: ['@commitlint/config-conventional'] };
七、Husky
安装
npm install lint-staged husky -D
在 package.json 中添加以下代码
{
...
"scripts": {
...
"prepare": "husky install",
...
}
...
}
执行初始化 husky 前需要先初始化 git 仓库
// 初始化 git 仓库
git init
// 初始化 husky
npm run prepare
// 或者
npx husky install
添加 commit-msg
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
上面命令执行成功后会在 .husky 目录下生成一个 commit-msg 文件,该文件的内容如下,表示在 git commit 前执行一下 npx --no -- commitlint --edit $1 指令,对 commit 的注释进行校验。以下为 commit-msg 内容
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no-install commitlint --edit $1
添加 pre-commit
npx husky add .husky/pre-commit "npm run lint"
npx husky add .husky/pre-commit "npm run lint:css"
npx husky add .husky/pre-commit "npm run lint:md"
上面命令执行成功后会在 .husky 目录下生成一个 pre-commit 文件,该文件的内容如下,表示在 git commit 前执行一下 npm run lint-staged 指令,对所有代码进行 eslint 校验 和 stylelint 校验 ,不符合校验规则就终止 commit。以下为 pre-commit 内容
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run lint
npm run lint:css
npm run lint:md
OK,到此项目安装完成。