React - Typescript eslint, prettier 개발 환경 구축
CRA(create-react-app) 시작하기
npm
으로 시작하기
npx create-react-app {프로젝트이름} --template typescript
yarn
으로 시작하기
yarn create react-app {프로젝트이름} --template typescript
절대경로 alias 설정
tsconfig.json의
compilerOptions
>baseUrl
,paths
속성 추가
// tsconfig.json
{
"compilerOptions": {
...
"baseUrl": "./src",
"paths": {
"@/*": ["*"] // src/App.tsx는 @/App.tsx로 경로 설정
},
...
}
}
webpack 구성 커스터마이징 위해
craco
설치, craco.config.js에 alias 추가
# npm
npm install -D @craco/craco
# yarn
yarn add -D @craco/craco
// craco.config.js
module.exports = {
webpack: {
alias: {
'@': path.resolve(__dirname, 'src/'),
},
},
}
ESLint와 Prettier
ESLint
,Prettier
설치
# npm
npm install -D eslint prettier
# yarn
yarn add -D eslint prettier
Prettier
가 적용하는 코드 스타일과 중복되는ESLint
규칙을 비활성화하여 충돌 방지
# npm
npm install -D eslint-config-prettier eslint-plugin-prettier
# yarn
yarn add -D eslint-config-prettier eslint-plugin-prettier
TypeScript
ESLint
parser & plugin
# npm
npm install -D @typescript-eslint/parser @typescript-eslint/eslint-plugin
# yarn
yarn add -D @typescript-eslint/parser @typescript-eslint/eslint-plugin
ESLint
에서TypeScript
모듈을 불러올 때 경로를 지정해주는 도구(default가 js이기 때문)
# npm
npm install -D eslint-import-resolver-typescript
# yarn
yarn add -D eslint-import-resolver-typescript
.eslintrc.json
{
"env": { "node": true, "browser": true },
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"typescript": {}
}
}
}
.prettierrc.json
{
"printWidth": 80,
"singleQuote": true,
"arrowParens": "avoid",
"semi": true,
"useTabs": false,
"trailingComma": "all",
"tabWidth": 2
}
* ESLint airbnb style 적용
패키지 다운로드
# npm
npm install -D eslint-config-airbnb eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-hooks
# yarn
yarn add -D eslint-config-airbnb eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-hooks
.eslintrc.json 수정
{
"env": { "node": true, "browser": true },
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"airbnb",
"airbnb/hooks",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"react/jsx-filename-extension": [
"warn",
{ "extensions": [".js", ".jsx", ".ts", ".tsx"] }
],
"import/no-unresolved": "error",
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"typescript": {}
}
}
}
Testing Library 설정
Jest 기준으로 절대 경로 alias 반영
// craco.config.js
module.exports = {
...
jest: {
configure: {
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
},
},
...
}
Last updated