源码仓库:github.com/Liangjiahon…
项目创建
- 目前
Vue 官方已经声明 Vue-CLI 在维护状态,更推荐使用 npm create 创建一个应用,使用该方式创建的Vue 应用默认是 Vite 脚手架的
pnpm create vue@latest
✔ Project name: 项目名称
✔ Add TypeScript? Yes
✔ Add JSX Support? Yes # 这里我选择支持JSX
✔ Add Vue Router for Single Page Application development? Yes # 集成路由
✔ Add Pinia for state management? Yes # 集成Pinia
✔ Add Vitest for Unit testing? … No # 我没选择单元测试
✔ Add an End-to-End Testing Solution? … No
✔ Add ESLint for code quality? … No / Yes # 集成ESlint
✔ Add Prettier for code formatting? … No / Yes # 集成Prettire
...
> cd 项目名称
> pnpm install
> pnpm dev
规范配置
- 由于项目已经集成了
Eslint、Prettier,那么就不需要自行安装了
- 这里仍需安装两个插件
pnpm i @typescript-eslint/parser vue-eslint-parser -D
@typescript-eslint/parser: 将 TypeScript 转换成与 estree 兼容的形式
vue-eslint-parser: 设置 Eslint 的解析器( Eslint 默认的解析器是 EStree )
Eslint配置文件
require("@rushstack/eslint-patch/modern-module-resolution");
module.exports = {
root: true,
env: {
browser: true,
es6: true,
node: true
},
parser: "vue-eslint-parser",
parserOptions: {
parser: "@typescript-eslint/parser",
ecmaVersion: "latest",
sourceType: "module",
jsxPragma: "Vue",
ecmaFeatures: { jsx: true }
},
extends: [
"plugin:vue/vue3-recommended",
"prettier",
"plugin:prettier/recommended",
"@vue/eslint-config-typescript",
"@vue/eslint-config-prettier/skip-formatting",
"./.eslintrc-auto-import.json"
],
plugins: ["vue"],
rules: {
"vue/script-setup-uses-vars": "warn",
"vue/multi-word-component-names": "off",
"vue/custom-event-name-casing": "off",
"vue/attributes-order": "off",
"vue/v-on-event-hyphenation": "off",
"vue/one-component-per-file": "off",
"vue/max-attributes-per-line": "off",
"vue/singleline-html-element-content-newline": "off",
"vue/attribute-hyphenation": "off",
"vue/require-default-prop": "off",
"@typescript-eslint/no-unused-vars": ["warn", { vars: "all", args: "none" }],
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-empty-function": "off",
"no-return-assign": "off",
"no-param-reassign": "off",
"guard-for-in": "off",
"no-unused-vars": ["warn", { vars: "all", args: "none" }],
"no-var": "error",
"no-new": "off",
"eol-last": "off",
"no-shadow": "off"
}
};
Prettier配置
module.exports = {
$schema: "https://json.schemastore.org/prettierrc",
useTabs: false,
semi: true,
tabWidth: 2,
singleQuote: false,
printWidth: 100,
trailingComma: "none",
bracketSameLine: false,
endOfLine: "lf",
bracketSpacing: true,
requirePragma: false,
insertPragma: false,
vueIndentScriptAndStyle: false,
proseWrap: "never",
tmlWhitespaceSensitivity: "strict",
embeddedLanguageFormatting: "auto"
};
Stylelint配置
module.exports = {
root: true,
extends: [
"stylelint-config-standard",
"stylelint-config-recommended-less",
"stylelint-config-standard-vue"
],
plugins: ["stylelint-order"],
rules: {
indentation: 2,
"selector-class-pattern": null,
"keyframes-name-pattern": null,
"no-empty-source": null,
"alpha-value-notation": "number",
"no-descending-specificity": null,
"unicode-bom": "never",
"font-family-no-missing-generic-family-keyword": null,
"declaration-colon-space-after": "always",
"declaration-colon-space-before": "never",
"number-max-precision": 2,
"unit-no-unknown": [true, { ignoreUnits: ["rpx"] }],
"selector-pseudo-element-no-unknown": [
true,
{ ignorePseudoElements: ["v-deep", "v-global", "v-slotted"] }
],
"selector-pseudo-class-no-unknown": [true, { ignorePseudoClasses: ["deep", "global"] }],
"at-rule-no-unknown": [
true,
{
ignoreAtRules: ["tailwind", "function", "if", "else", "else-if", "each", "include", "mixin"]
}
],
"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",
"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",
"line-height",
"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",
"overflow",
"overflow-x",
"overflow-y",
"opacity",
"filter",
"list-style",
"outline",
"visibility",
"box-shadow",
"text-shadow",
"resize",
"transition",
"content"
]
},
overrides: [
{
files: ["**/*.(less|css|vue|html)"],
customSyntax: "postcss-less"
},
{
files: ["**/*.(html|vue)"],
customSyntax: "postcss-html"
}
]
};
TS配置文件
tsconfig.json 是 TypeScript 项目中的配置文件,用于指定编译器的行为和编译选项
- 它可以用来配置
TypeScript 编译器编译项目的各种设置,包括编译目标、模块系统、文件路径、代码质量检查等等
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"importHelpers": true,
"resolveJsonModule": true,
"noUnusedLocals": false,
"noUnusedParameters": true,
"experimentalDecorators": true,
"noImplicitAny": false,
"skipLibCheck": true,
"baseUrl": "./",
"lib": ["esnext", "dom", "dom.iterable", "scripthost"],
"types": ["node"],
"typeRoots": ["./node_modules/@types/", "./types"],
"paths": {
"@/*": ["./src/*"],
"@img/*": ["./src/assets/img/*"],
"#": ["./types/*"]
}
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue",
"types/**/*.d.ts",
"types/**/*.ts",
"build/**/*.ts",
"build/**/*.d.ts",
"mock/**/*.ts",
"vite.config.ts"
],
"exclude": ["node_modules", "dist", "**/*.js"]
}
项目其他配置
项目目录结构
├── build # 构建相关
│ ├── config # 配置文件
│ └── vite # vite配置
├── mock # mock接口
├── public # 公共静态资源目录
├── src # 主目录
│ ├── api # 接口文件
│ ├── assets # 资源文件
│ ├── components # 公共组件
│ ├── design # 样式文件
│ ├── directives # 自定义指令
│ ├── enums # 枚举/常量
│ ├── hooks # hook
│ ├── layouts # 布局文件
│ ├── locales # 多语言
│ ├── main.ts # 主入口
│ ├── router # 路由配置
│ ├── settings # 项目配置
│ ├── store # 数据仓库
│ ├── utils # 工具类
│ └── views # 页面
├── types # 全局共享类型配置
└── vite.config.ts # vite配置文件