$ npm init vue@latest
注意:此时并不代表你选择了npm包管理器,选择具体哪个包管理器,是在首次安装node_modules的时候
本次项目使用pnpm包管理器
$ pnpm i
集成ESLint一般和prettier使用,也可以使用ESLint自带的代码风格规范。本次项目没有使用开发者自定义配置的.eslintrc.js和.prettierrc.js。而是借助第三方插件,该插件内置了ESLint的常用配置项。采用了ESLint的To check syntax, find problems, and enforce code style(检查语法、发现问题并强制执行代码风格)
ESLint
// @antfu/eslint-config 是antfu这个人封装好的一套ESLint插件,内置了"To check syntax, find problems, and enforce code style"。所以不需要再使用.eslintrc.js 和 prettierrc的配置了// @antfu/eslint-config基于eslint
$ pnpm i eslint @antfu/eslint-config -D
@antfu/eslint-config
{"extends": "@antfu","rules": {// 关掉保存文件后, 内容 分行显示"vue/singleline-html-element-content-newline" : "off"}
}
{// 禁用prettier插件"prettier.enable": false,// 禁用保存文件自动格式化"editor.formatOnSave": false,"editor.codeActionsOnSave": {"source.fixAll":true,"source.fixAll.eslint": true,},
}
或者在VScode工具中的setting,json添加以上内容
"lint:eslint": "eslint src/**/*.{ts,tsx,js,jsx,vue} --fix",
$ pnpm add sass postcss postcss-html stylelint stylelint-config-standard stylelint-config-standard-scss stylelint-config-standard-vue stylelint-scss -D
.stylelintrc
{"extends": ["stylelint-config-standard","stylelint-config-standard-scss","stylelint-config-standard-vue/scss"],"plugins": ["stylelint-scss"],"rules": {"selector-class-pattern": ["^([a-z][a-z0-9]*)(-[a-z0-9]+)*$",{"message": "Expected class selector to be kebab-case"}],"declaration-empty-line-before": null,"no-descending-specificity": true,"color-hex-length": ["long","short"],"at-rule-no-unknown": null}
}
.stylelintignore
dist/
node_modules/
{"editor.codeActionsOnSave": {"source.fixAll.stylelint": true},// 配置stylelint检查的文件类型范围"stylelint.validate": ["css","less","postcss","scss","sass","vue"],"stylelint.enable": true,"css.validate": false,"less.validate": false,"scss.validate": false,"typescript.tsdk": "node_modules\\typescript\\lib"
}
"lint:stylelint": "stylelint src/**/*.{css,scss,vue} --fix"
$ npx husky-init '&&' pnpm install
项目根目录下,生成了.husky目录
pnpm i lint-staged -D
"lint-staged": {"src/**/*.{ts,tsx,js,jsx,vue}": "eslint --fix","src/**/*.{css,scss,vue}": "stylelint --fix"
},
npx lint-staged --allow-empty $1
.editorconfig 为了多人开发时,兼容不同的开发工具
# http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
{"name": "reportal","version": "0.0.0","private": true,"engines": {"node": ">= 16"},"scripts": {"dev": "vite","build": "run-p type-check build-only","preview": "vite preview","build-only": "vite build",// pnpm lint 时,同时run了type-check lint:eslint lint:stylelint三种命令"lint": "run-p type-check lint:eslint lint:stylelint","type-check": "vue-tsc --noEmit","lint:eslint": "eslint src/**/*.{ts,tsx,js,jsx,vue} --fix","lint:stylelint": "stylelint src/**/*.{css,scss,vue} --fix",// 安装了husky 新增的"prepare": "husky install",// 项目中只允许使用pnpm"preinstall": "npx only-allow pnpm"},"dependencies": {...},"devDependencies": {...},// 为了让lint-staged出发git hook"lint-staged": {"src/**/*.{ts,tsx,js,jsx,vue}": "eslint --fix","src/**/*.{css,scss,vue}": "stylelint --fix"}
}
elemet-plus官网
$ pnpm i element-plus
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import App from './App.vue'import 'element-plus/dist/index.css'
const app = createApp(App)app.use(ElementPlus)
app.mount('#app')
"compilerOptions": {// 显示 element-plus 中的组件类型 及其 属性提示"types": ["element-plus/global"],},
env.d.ts本来在项目目录下,将这个文件如果移到types文件夹下,需要更改tsconfig.json的配置
"include": ["types/**.d.ts", "src/**/*", "src/**/*.vue"],
分离vite.config.ts中的配置的时候,需要在tsconfig.node.json文件进行如下说明
"include": ["vite.config.*", "vitest.config.*", "cypress.config.*", "playwright.config.*","vite/**/*.ts"],
自动引入组件,即是写上组件名,保存文件,不需要在具体文件上写对应的import语句了
pnpm install -D unplugin-vue-components unplugin-auto-import
components.ts
import components from 'unplugin-vue-components/vite'
// 使用src下的components 不用再import
export default function setComponents() {return components({dirs: ['src/components'],include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/],dts: 'types/components.d.ts',})
}
auto-import.ts
// 找不到模块“unplugin-auto-import”或其相应的类型声明。
// import AutoImport from 'unplugin-auto-import'
// 解决
import AutoImport from 'unplugin-auto-import/vite'
export default function setAutoImport() {return AutoImport({// 使用'vue', 'vue-router', 'pinia'时,不再需要导入了imports: ['vue', 'vue-router', 'pinia'],dts: 'types/auto-imports.d.ts',})
}
import type { PluginOption } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from './auto-import'
import components from './components'
export default function setVitePlugins() {const plugins: PluginOption[] = [vue(), vueJsx()]plugins.push(AutoImport())plugins.push(components())return plugins
}