专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

《前端研发规范》+配置文件_前端开发规范

ins518 2025-09-28 01:11:04 技术文章 2 ℃ 0 评论

我帮你把刚才那份 《前端研发规范(生产级)》 配上可直接落地的工程化配置文件,这样规范不仅是纸面要求,还能在项目里自动执行,避免靠人记忆。你可以把这些文件直接放到项目根目录,配合 CI/CD 或本地 Git 钩子使用。


配套工程化配置

1. ESLint 配置(.eslintrc.cjs)

module.exports = {
  root: true,
  env: { browser: true, es2022: true, node: true },
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: ['@typescript-eslint', 'vue', 'import'],
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:import/recommended',
    'plugin:import/typescript',
    'prettier'
  ],
  rules: {
    'vue/multi-word-component-names': 'off',
    'import/order': ['error', {
      'newlines-between': 'always',
      groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index']
    }],
    '@typescript-eslint/no-explicit-any': 'warn',
    'no-console': ['warn', { allow: ['error'] }],
    'max-lines': ['warn', { max: 500 }],
    complexity: ['warn', { max: 15 }]
  },
  settings: { 'import/resolver': { typescript: true } }
};

2. Prettier 配置(.prettierrc)

{
  "singleQuote": true,
  "semi": true,
  "trailingComma": "all",
  "printWidth": 100,
  "tabWidth": 2,
  "endOfLine": "lf"
}

3. Stylelint 配置(.stylelintrc.json)

{
  "extends": ["stylelint-config-standard"],
  "rules": {
    "string-quotes": "single",
    "color-hex-length": "short",
    "selector-class-pattern": "^[a-z0-9\\-]+#34;,
    "max-nesting-depth": 3
  }
}

4. Commitlint 配置(commitlint.config.cjs)

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'header-max-length': [2, 'always', 100],
    'subject-case': [2, 'never', ['sentence-case', 'start-case', 'pascal-case', 'upper-case']]
  }
};

5. Husky + lint-staged(package.json 片段)

{
  "scripts": {
    "prepare": "husky install"
  },
  "lint-staged": {
    "src/**/*.{ts,vue,js}": [
      "eslint --fix",
      "prettier --write"
    ],
    "src/**/*.{css,scss}": [
      "stylelint --fix"
    ]
  }
}

初始化 Husky:

npm run prepare
npx husky add .husky/pre-commit "npx lint-staged"

6. Vitest 测试配置(vitest.config.ts)

import { defineConfig } from 'vitest/config';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [vue()],
  test: {
    globals: true,
    environment: 'jsdom',
    coverage: {
      provider: 'c8',
      reporter: ['text', 'html'],
      lines: 80,
      functions: 80,
      branches: 80,
      statements: 80
    }
  }
});

7. 安全与依赖扫描(CI 示例)

在 CI 流程中增加:

- name: 安全扫描
  run: npm audit --audit-level=high

- name: 依赖更新检查
  run: npx npm-check-updates

使用建议

  1. 本地开发:开发者提交代码前会自动执行 ESLint、Prettier、Stylelint,保证风格统一。
  2. CI 阶段:执行 Lint、Test、Build、安全扫描,确保代码质量和安全。
  3. 覆盖率门槛:Vitest 配置了 80% 覆盖率要求,低于此值 CI 直接失败。
  4. 提交信息:Commitlint 强制执行 feat/fix/docs/refactor 等前缀,并限制长度和格式。

如果你愿意,我可以帮你把 《前端研发规范》+这些配置文件 打包成一个可直接初始化新项目的模板仓库,这样新项目直接 git clone 就能用,规范和工具链全套到位。这样团队落地成本最低。
你要我帮你做这个模板仓库的目录和初始化脚本吗?这样你们以后新项目一键起步。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表