前端工程化最佳實踐

1、代碼格式化規範

目前項目中使用的 vetur 插件內置有 prettier 格式化,也能夠安裝 prettier code formatter 插件,eslint 也包含部分代碼風格檢查的功能,eslint 和 prettier 自己就有部分規則是衝突的,致使格式化混亂,因此必須統一代碼格式化規範css

一、vscode 中的配置優先級

  • 默認配置文件(優先級最低)
  • 用戶配置文件(優先級次之)
  • 工程配置文件 (優先級最高)

爲了統一你們的代碼風格,統一使用項目中的配置文件做爲配置項。因爲 ESLint 的主要功能是代碼質量檢查,Prettier 的主要功能是代碼風格檢查,因此不要在 ESLint 中去配置代碼風格相關的規則。html

  • prettier。 一個很流行的代碼格式化工具,你很容易在編輯器找到實現它的各類插件,這裏用它在代碼提交前作代碼格式化。
  • eslint。 代碼檢查工具。eslint 也能夠負責一部分代碼格式檢查的工做,可是 prettier 已經作的很好了,因此我便沒用 eslint 的代碼格式檢查,只讓其負責代碼錯誤檢查。

二、解決配置衝突

npm i eslint-config-prettier  eslint-plugin-prettier -D

eslint-config-prettier 關閉 Eslint 中與 Prettier 衝突的選項,eslint-plugin-prettier 將 prettier 的規則設置爲 eslint 的規則,對不符合規則的進行提示vue

三、prettierrc 配置文件說明

//.prettierrc.js
module.exports = {
    printWidth: 160, //編輯器每行的長度,默認80
    tabWidth: 4, //製表符tab的寬度,默認值是2
    useTabs: false, //代碼縮進是否用製表符tab,默認false
    semi: true, //是否使用分號,默認true,使用分號
    singleQuote: true, //是否使用單引號,默認爲false
    quoteProps: 'as-needed', //對象屬性的引號使用 as-needed 僅在須要的時候使用 consistent 有一個屬性須要引號,就都須要引號 preserve 保留用戶輸入的狀況
    jsxSingleQuote: false,
    trailingComma: 'none', //末尾逗號 none 末尾沒有逗號 es5 es5有效的地方保留 all 在可能的地方都加上逗號
    bracketSpacing: true, //字面量對象括號中的空格,默認true true - Example: { foo: bar }.  false - Example: {foo: bar}.
    jsxBracketSameLine: false,
    arrowParens: 'avoid', //箭頭函數中的括號always avoid
    htmlWhitespaceSensitivity: 'ignore',
    vueIndentScriptAndStyle: false,//是否給vue中的 <script> and <style>標籤加縮進
    endOfLine: 'auto', //行末尾標識
    eslintIntegration: true, //不讓prettier使用eslint的代碼格式進行校驗
}

四、eslint 配置文件說明

//.eslintrc.js
module.exports = {
    root: true,
    env: {
        node: true
    },
    'extends': [
        'plugin:vue/essential',
        "plugin:prettier/recommended",
        // '@vue/standard'
    ],
    rules: {
        'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        // 'vue/script-indent': ['error', 4, { 'baseIndent': 1 }],
        // "quotes": [2, "single", { "avoidEscape": true }],
        // 使用prettier來替換eslint的規則
        "prettier/prettier": "error",
        "no-var": 2,//禁用var,用let和const代替
        "no-unused-vars": [2, { "args": "none" }],  //消除未使用的變量  不檢查函數的參數
        "no-redeclare": 2, //禁止屢次聲明同一變量
        "no-dupe-keys": 2,//在建立對象字面量時不容許鍵重複
        'eqeqeq': ['error', 'always', { null: 'ignore' }], // 強制使用全等
    },
    parserOptions: {
        parser: 'babel-eslint',
        "ecmaVersion": 6,
        "sourceType": "module"
    }
}

3、代碼提交規範

一、安裝 husky 和 lint-stage

//husky新版本配置方法徹底不同,這裏鎖定版本號
npm i husky@4.2.5 lint-stage -D

Husky 可以阻止不規範的代碼提交和推送,確保本地的代碼已經經過檢查才能 push 到遠程。node

lint-stage 的做用是隻對當前修改後的文件進行掃描,即進行 git add 加入到 stage 區的文件進行掃描便可,完成對增量代碼進行檢查webpack

二、配置 commitlint 提交規則

npm i @commitlint/cli @commitlint/config-conventional -D
//commitlint.config.js
// https://commitlint.js.org/#/
module.exports = {
    extends: [
        "@commitlint/config-conventional"
    ],
    rules: {
        // 'name:[level, 'always', 72]',level可選0, 1, 2,0爲disable,1爲warning,2爲error,第二位爲應用與否,可選always| never,第三位該rule的值
        // update: 更新某功能(不是feat, 不是fix)
        // feat: 新增功能(feature)
        // fix: bug 修復
        // style: 樣式調整
        // merge:分支合併
        // revert:回滾某個更早以前的提交

        // build:主要目的是修改項目構建系統(例如 glup,webpack,rollup 的配置等)的提交
        // ci:主要目的是修改項目繼續集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交
        // docs:文檔更新
        // perf:性能, 體驗優化
        // refactor:重構代碼(既沒有新增功能,也沒有修復 bug)
        // test:新增測試用例或是更新現有測試
        // chore:不屬於以上類型的其餘類型
        'type-enum': [2, 'always', ['update', 'feat', 'fix', 'style', 'merge', 'revert', 'build', 'ci', 'docs', 'perf', 'refactor', 'test', 'chore']],
        'type-case': [0], //type小寫
        'type-empty': [0], //type不爲爲空
        'scope-empty': [0],
        'scope-case': [0],
        'subject-full-stop': [0, 'never'],
        'subject-case': [0, 'never'],
        'header-max-length': [0, 'always', 72]
    }
};

三、配置 package.json 文件

{
  "name": "name",
  "version": "0.1.0",
  "description": "description",
  "author": "author",
  "private": true,
  "scripts": {
    "dev": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint -e $HUSKY_GIT_PARAMS"
    }
  },
  "lint-staged": {
    "src/**/*.{js,jsx,vue,json,css,less,scss,sass}": [
      "prettier --write",
      "eslint --fix",
      "git add"
    ]
  },
  "dependencies": {
    "axios": "^0.19.0",
    "core-js": "^2.6.5",
    "element-ui": "^2.12.0",
    "md5": "^2.2.1",
    "vue": "^2.6.10",
    "vue-router": "^3.0.3",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@commitlint/cli": "^12.1.4",
    "@commitlint/config-conventional": "^12.1.4",
    "@vue/cli-plugin-babel": "^3.5.0",
    "@vue/cli-plugin-eslint": "^3.5.0",
    "@vue/cli-plugin-unit-mocha": "^3.5.0",
    "@vue/cli-service": "^3.5.3",
    "@vue/eslint-config-standard": "^4.0.0",
    "@vue/test-utils": "1.0.0-beta.29",
    "babel-eslint": "^10.0.1",
    "babel-plugin-component": "^1.1.1",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "compression-webpack-plugin": "^2.0.0",
    "eslint": "^5.8.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-vue": "^5.0.0",
    "husky": "^4.2.5",
    "lint-staged": "^11.0.0",
    "node-sass": "^4.9.0",
    "sass-loader": "^7.1.0",
    "uglifyjs-webpack-plugin": "^2.1.2",
    "vue-template-compiler": "^2.5.21"
  }
}

四、提交代碼

husky 會在你提交前,調用 pre-commit 鉤子,執行 lint-staged,若是代碼不符合 prettier 配置的規則,會進行格式化;而後再用 eslint 的規則進行檢查,若是有不符合規則且沒法自動修復的,就會中止這次提交。若是都經過了就會講代碼添加到 stage,而後 commit。ios

git add .
git commit -m "feat: commit內容"
git push
相關文章
相關標籤/搜索