以下是现代前端项目的完整代码规范配置方案,涵盖主流技术栈和自动化工具链配置:
project/
├── src/
│ ├── assets/ # 静态资源
│ ├── components/ # 通用组件
│ ├── layouts/ # 布局组件
│ ├── router/ # 路由配置
│ ├── store/ # 状态管理
│ ├── styles/ # 全局样式
│ ├── utils/ # 工具函数
│ └── views/ # 页面组件
├── .editorconfig # 编辑器统一配置
├── .eslintrc.js # ESLint 配置
├── .prettierrc # Prettier 配置
├── .stylelintrc.js # Stylelint 配置
└── .commitlintrc.js # Git 提交规范
.eslintrc.js
)module.exports = {
root: true,
env: { browser: true, es2021: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:vue/vue3-recommended', // Vue项目添加
'prettier'
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
// 核心规则
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'max-depth': ['error', 4], // 最大嵌套层级
// TypeScript 规则
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-ts-comment': 'warn',
// Vue 专用规则
'vue/multi-word-component-names': 'off',
'vue/html-self-closing': ['error', {
html: { void: 'always' }
}]
},
overrides: [
{
files: ['*.vue'],
rules: {
'max-lines-per-function': 'off'
}
}
]
};
.prettierrc
){
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "consistent",
"trailingComma": "none",
"bracketSpacing": true,
"arrowParens": "avoid",
"vueIndentScriptAndStyle": true,
"htmlWhitespaceSensitivity": "ignore"
}
.stylelintrc.js
)module.exports = {
extends: [
'stylelint-config-standard',
'stylelint-config-recommended-scss',
'stylelint-config-prettier'
],
plugins: ['stylelint-order'],
rules: {
'selector-class-pattern': '^[a-z][a-z0-9]*(-[a-z0-9]+)*$', // 短横线命名
'order/properties-order': [
'position',
'top',
'display',
'flex-direction', // 按逻辑分组排序
'width',
'height',
'margin',
'padding',
'color',
'background'
],
'max-nesting-depth': 3, // 最大嵌套层级
'scss/at-import-partial-extension': 'never'
}
};
// Good
.user-profile {
&__avatar { ... }
&__name--highlight { ... }
}
// Bad
.userProfile {
.avatar { ... }
.nameHighlight { ... }
}
// TypeScript 类型定义
interface Props {
/** 用户ID */
userId: number
/** 是否显示详情 */
showDetail?: boolean
}
const props = defineProps<Props>()
.commitlintrc.js
)module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert']
],
'subject-case': [0]
}
};
# 安装工具
npm install -g commitizen cz-conventional-changelog
# 提交示例
git commit -m "feat(user): add login functionality"
// package.json
{
"scripts": {
"prepare": "husky install",
"lint": "npm run lint:js && npm run lint:style",
"lint:js": "eslint --ext .js,.vue src",
"lint:style": "stylelint src/**/*.{css,scss,vue}"
},
"lint-staged": {
"*.{js,vue}": ["eslint --fix", "prettier --write"],
"*.{css,scss}": ["stylelint --fix", "prettier --write"]
}
}
# 创建 pre-commit hook
npx husky add .husky/pre-commit "npx lint-staged"
# 创建 commit-msg hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
命名规范:
代码组织:
性能优化:
// 组件懒加载
const UserProfile = () => import('./UserProfile.vue')
// 图片懒加载
<img v-lazy="imageUrl" />
文档规范:
/**
* 格式化日期
* @param {Date} date - 需要格式化的日期对象
* @param {string} format - 格式字符串
* @returns {string} 格式化后的日期字符串
*/
function formatDate(date, format = 'YYYY-MM-DD') {
// ...
}
该配置方案适用于 Vue3 + TypeScript + Vite 技术栈,可根据项目需求调整扩展规则。建议搭配 VSCode 的 ESLint、Prettier 插件实现实时校验。