[FE/Git] lint-staged

  • https://github.com/lint-staged/lint-staged
  • Ver. 15.5.0 기분
  • lint/prettier 등 규칙 대상이 Git 스테이징 상태에 있는 파일들을 대상으로 실행함
  • husky 설정에 이어짐 / https://royleej9.tistory.com/entry/FEGit-husky
  • husky만 설정할 경우 .git 폴더와 package.json 파일의 위치가 다를 경우 prettier 명령어 설정에서 대상 파일의 경로를 지정해야하는 불편함이 있음
    • husky 설정 주의 사항 참고

작업 순서

  1. 설치
  2. 기본 설정

1. 설치

npm i -D lint-staged

2. 기본 설정

1. ./husky/pre-commit 수정

수정 전

npx prettier --cache --ignore-path ./frontend/.prettierignore --write ./frontend

수정 후

npx lint-staged

2. lint-staged.config.js 설정

  • package.json이 있는 위치에 해당 파일 생성
/**
 * @filename: lint-staged.config.js
 * @type {import('lint-staged').Configuration}
 */
module.exports = {
  '*.{js,jsx,ts}': [
    'npx prettier --write',
  ],
};
  • git commit을 실행하면 스테이징 상태에 있는 파일 만을 대상으로만 명령을 실행 후 commit 실행

+ Recent posts