【记录】项目文件行尾样式统一设置为lf

59 阅读1分钟

windows下文件行尾样式默认为crlf, mac、linux下为lf。行尾样式不一致会导致意想不到的问题,多人协作开发,git甚至会反复检测文件变更。

在项目根目录添加.gitattributes

指定文件格式为lf,使git不再自动转换文件格式为crlf。不要改变图片。

* text eol=lf
*.png -diff -text
*.jpg -diff -text
*.jpeg -diff -text
*.gif -diff -text
*.bmp -diff -text
*.tiff -diff -text
*.tif -diff -text
*.ico -diff -text
*.webp -diff -text
*.svg -diff -text

git commit格式化文件

安装配置prettier,如下:
{
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 100,
  "tabWidth": 2,
  "useTabs": false,
  "semi": true,
  "endOfLine": "lf",
  "overrides": [
    {
      "files": ".prettierrc",
      "options": { "parser": "json" }
    }
  ]
}

配置 husky、lint-staged

安装husky、lint-staged完成后,执行以下命令,初始化husky

# npx husky init
pnpm exec husky init

根目录生成.husky目录后 将以下脚本复制到.husky/pre-commit文件

#!/bin/sh
npx lint-staged

在package.json配置(根据项目实际自行调整)

"lint-staged": {
	"*.vue": [
      "eslint --fix --max-warnings=0",
      "prettier --write"
    ],
    "*.{js,ts,jsx,tsx}": [
      "eslint --fix --max-warnings=0",
      "prettier --write"
    ],
    "*.{css,scss}": [
      "prettier --write"
    ],
    "*.{html,md,json}": [
      "prettier --write"
    ]
}