🖥Frontend/Next.js

Next.js TS Eslint 와 Prettier 동시에 셋업하기

코너(Corner) 2023. 9. 5.
반응형

Next.js TS Eslint 와 Prettier 동시에 셋업하기

Eslint 설치

yarn add -D eslint-config-airbnb-typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser

npx install-peerdeps --dev eslint-config-airbnb

Prettier 설치

yarn add -D prettier eslint-plugin-prettier eslint-config-prettier

.eslintrc.json 파일 수정하기

{
    "root": true,
    "parser": "@typescript-eslint/parser",
    "plugins": ["@typescript-eslint", "prettier"],
    "parserOptions": {
        "project": ["./tsconfig.json", "next.config.js"],
        "createDefaultProgram": true
    },
    "env": {
        // 전역객체를 eslint가 인식하는 구간
        "browser": true,
        // document나 window 인식되게 함
        "node": true,
        "es6": true
    },
    "ignorePatterns": ["node_modules/", "postcss.config.js"],
    // eslint 미적용될 폴더나 파일 명시
    "extends": [
        "airbnb",
        "airbnb-typescript",
        "airbnb/hooks",
        "next/core-web-vitals",
        "plugin:@typescript-eslint/recommended",
        // ts 권장
        "plugin:prettier/recommended",
        // eslint의 포매팅을 prettier로 사용.
        "prettier"
        // eslint-config-prettier prettier와 중복된 eslint 규칙 제거
    ],
    "rules": {
        "react/button-has-type": "off",
        "react/react-in-jsx-scope": "off", // react 17부턴 import 안해도돼서 기능 끔
        // 경고표시, 파일 확장자를 .ts나 .tsx 모두 허용함
        "react/jsx-filename-extension": [
            "warn",
            {
                "extensions": [".ts", ".tsx"]
            }
        ],
        "no-useless-catch": "off" // 불필요한 catch 못쓰게 하는 기능 끔
    }
}

.prettierrc 파일을 Root 경로에 생성하고 수정하기

{
    "singleQuote": true,
    "semi": true,
    "useTabs": false,
    "tabWidth": 4,
    "trailingComma": "all",
    "printWidth": 120,
    "overrides": [
        {
            "files": ["*.js", "*.ts", "*.tsx", "*.jsx"],
            "options": {
                "semi": false
            }
        },
        {
            "files": "*.json",
            "options": {
                "semi": false,
                "bracketSpacing": true,
                "trailingComma": "all"
            }
        }
    ]
}

.prettierignore 파일 수정하기

#
postcss.config.js
node_modules/
.next/

프로젝트 종료 후 재시작

반응형

'🖥Frontend > Next.js' 카테고리의 다른 글

Next.js - 1. 개념정리  (0) 2023.09.01

댓글