지금 만드는 토이프로젝트에 tslint, eslint, prettier을 적용한 겸 세팅값을 블로그에도 적어둔다.
CRA로 만든 프로젝트에 따로 webpack config를 eject 하지 않고 위에 3개를 적용할 수 있는 방법임.
- Refer: https://dev.to/benweiser/how-to-set-up-eslint-typescript-prettier-with-create-react-app-3675
- 오픈소스에 적용한 커밋: https://github.com/milooy/TellMe/commit/d2c5520b9d4c0ebfac2629ca2f02066afbeb61b8
Step 1: 온갖 devDependencies를 설치한다.
npm i -D @types/react typescript-eslint/eslint-plugin typescript-eslint/parser eslint eslint-config-prettier eslint-config-react eslint-loader eslint-plugin-prettier prettier tslint typescript
다만 여러분이 이 글을 보는 시점에 https://github.com/prettier/prettier-atom/issues/505#issuecomment-516791742 이 Issue가 해결되지 않았다면 eslint는 5.16.0 버전으로 설치한다(19년 9월 기준으로 v6임)
// eslint를 5.16.0 으로 박아둔 버전 npm i -D @types/react typescript-eslint/eslint-plugin typescript-eslint/parser eslint@5.16.0 eslint-config-prettier eslint-config-react eslint-loader eslint-plugin-prettier prettier tslint typescript
왜냐면 에디터 prettier에서 dependency 에러가 나더라. Failed to load plugin 'prettier' declared in 'CLIOptions': Cannot find module 'eslint-plugin-prettier'
라고 나옴. eslint를 5.16버전으로 다운그레이딩하면 이 에러가 사라진다. 곧 Fix PR 만든다고 했으니 미래에는 에러 해결이 되었길…
내 package.json은 다음과 같다
"devDependencies": { "@types/react": "^16.9.2", "@typescript-eslint/eslint-plugin": "^2.3.0", "@typescript-eslint/parser": "^2.3.0", "eslint": "^5.16.0", "eslint-config-prettier": "^6.3.0", "eslint-config-react": "^1.1.7", "eslint-loader": "^3.0.0", "eslint-plugin-prettier": "^3.1.1", "prettier": "^1.18.2", "tslint": "^5.20.0", "typescript": "^3.6.3" },
Step 2: eslint 셋업을 하는 .eslintrc.json 파일을 만든다
eslint의 세부사항을 셋업할 수 있는 파일을 루트폴더에 생성한다. 잘 보면 타입스크립트와 프리티어 세팅 모두 박혀있다.
.eslintrc.json
{ "extends": [ "eslint:recommended", "plugin:react/recommended", "plugin:@typescript-eslint/recommended", "prettier/@typescript-eslint", "plugin:prettier/recommended" ], "plugins": ["react", "@typescript-eslint", "prettier"], "env": { "browser": true, "jasmine": true, "jest": true }, "settings": { "react": { "pragma": "React", "version": "detect" } }, "parser": "@typescript-eslint/parser" }
Step 3: 몇몇 파일에 eslint를 disable시킬 .eslintignore 파일을 만든다
서비스워커 파일이나 tests 폴더나 legacy 소스들에는 eslint를 끄고싶은 분들도 있을거다. ignore할 수 있는 파일을 만든다.
.eslintignore
src/registerServiceWorker.js src/**/__tests__/**
Step 4: 타입스크립트 린트 세팅 파일인 tslint.json을 만든다
TSlint 셋업을 해준다. 요거 키면 .tsx 파일들에 빨간줄이 무지막지하게 나온다.
tslint.json
{ "defaultSeverity": "error", "extends": [ "tslint:recommended" ], "jsRules": {}, "rules": { "quotemark": false }, "rulesDirectory": [] }
Step 5: 예쁘게 코드 포매팅을 해주는 프리티어 세팅 파일인 .prettierrc 를 만든다
이렇게 프리티어 세팅을 해주고 VSCode에서 mac기준 shift + option + f를 누르면 세팅값대로 코드를 예쁘게 성형해준다. 입맛대로 세팅을 해준다. 프리티어 다큐멘테이션 참고하삼 https://prettier.io/docs/en/options.html 아래는 제 세팅
.prettierrc
{ "singleQuote": true, "semi": true, "useTabs": false, "tabWidth": 2, "trailingComma": "all", "printWidth": 80, "arrowParens": "always", "orderedImports": true }
(optional) Step 6: 이제 다했당. package.json에 eslint 바로 고쳐주는 스크립트를 짜둔다
lint:fix
란 이름으로 eslint fix 스크립트를 짜두면 언제든 npm run lint:fix
명령어로 린트 픽스를 할 수 있다. (하지만 난 파일마다 수동으로 단축키를 통해 고치는걸 선호하지)
"scripts": { ... "lint:fix": "eslint './src/**/*.{ts,tsx}'" }
끗!
이제 열시미 js랑 tsx 파일 린트에러를 고치면 됩니다.
적용한 파일은 https://github.com/milooy/TellMe/commit/d2c5520b9d4c0ebfac2629ca2f02066afbeb61b8 참고