1. 配置

  • yarn add —dev —exact prettier
  • echo {}> .prettierrc.json
  • 新建 .prettierignore
    1. build
    2. coverage

lint-staged

  • 我们想要每次git commit的时候,自动运行prettier,需要借助pre-commit hook
  • npx mrm lint-staged
    • 会发现package.json中新添了”husky”和”lint-staged”命令
      1. "husky": {
      2. "hooks": {
      3. "pre-commit": "lint-staged"
      4. }
      5. },
      6. "lint-staged": {
      7. "*.{js,css,md,ts,tsx}": "prettier --write"
      8. }

eslint-config-prettier

  • prettier和eslint一起工作的时候会有些冲突,我们需要借助一个包(eslint-config-prettier)来解决这个问题
  • yarn add -D eslint-config-prettier
  • 在package.json里的”eslintConfig”中加入”prettier” ```json “eslintConfig”: { “extends”: [ …,
  • “prettier” ] } ```

commitlint

规范git commit message 规则:https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-conventional

  • yarn add -D @commitlint/{config-conventional,cli}
  • echo “module.exports = {extends: [‘@commitlint/config-conventional’]}” > commitlint.config.js
  • 在package.json的”husky”里加上”commit-msg”
    1. "husky": {
    2. "hooks": {
    3. "pre-commit": "lint-staged",
    4. "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    5. }
    6. }