yarn create vite ./ --template react-ts
일반적으로 Lintting 기능은 ESLint에, Code Formatting은 Prettier에 일임하는 방식으로 사용한다.
git init
yarn add react-router-dom
yarn add eslint -D
yarn add prettier -D
yarn create @eslint/config
config 설정을 해준다 (원하는 형태로 Yes, No)
~~ 위에 생략
"plugins": ["react", "@typescript-eslint"],
"rules": {
"no-var": "error",
"no-multiple-empty-lines": "error",
"no-console": ["warn", { "allow": ["error"] }],
"eqeqeq": "error",
"dot-notation": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object",
"type"
],
"pathGroups": [
{
"pattern": "@/*",
"group": "internal",
"position": "after"
}
],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive": true
}
}
]
}
yarn add -D eslint-plugin-import
//.eslintrc.json extends, plugin 설정
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript",
"prettier"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint", "import"],
yarn add -D eslint-config-prettier
- eslint는 linting 기능을, prettier는 formatting을 담당하는 구조가 이상적
- 하지만, eslint에는 일부 formatting 관련된 rule도 포함되어 있음
- 이 rule들이 prettier와 다른 설정을 가지고 있다면 설정 충돌이 발생함
- 따라서, eslint에서 formatting 관련 rule들을 모두 해제해야할 필요가 있음 수
동으로 진행할 수도 있지만, 이를 적용해주는 eslint plugin이 존재
.prettierrc.cjs
루트 폴더에 생성
module.exports = {
bracketSpacing: true,
singleQuote: true,
tabWidth: 2,
trailingComma: "all",
printWidth: 100,
endOfLine: "auto",
useTabs: false,
semi: true,
jsxSingleQuote: true,
arrowParens: "always",
};
git hook 설정을 도와주는 npm package.
husky는 git에서 특정 이벤트(커밋, 푸쉬 등)을 실행할 때 이벤트에 hooks를 설정하여 hooks에 설정된 스크립트를 실행할 수 있다.
line-staged는 git의 staged된 상태에 파일들에 특정 명령어를 실행할 수 있도록 해주는 도구이며
line-staged는 Staged된 파일을 수정한 후 다시 git add를 하지 않아도 문제가 없도록 도와주는 도구다.
yarn add -D husky lint-staged
//package.json devDependencies: { ~~ },콤마 뒤에 추가해준다.
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"prettier --cache --write",
"eslint --cache --fix --max-warnings=0"
]
}
// package.json
{
"scripts": {
"postinstall": "husky install"
},
}
// package.json
{
"scripts": {
"postinstall": "husky install",
"format": "prettier --cache --write .",
"lint": "eslint --cache .",
},
}
yarn husky install
해줘야 husky 폴더가 생긴다.
npx husky add .husky/pre-commit "yarn lint-staged"
npx husky add .husky/pre-commit "yarn format"
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
paths 설정을 하면 아래처럼 import가 가능하다.
import { chartDataOptions, chartOptions } from '@/utils/chartOptions';
vite 에서는 추가로 설정해주어야 한다. 안그러면 에러가 발생.
yarn add -D vite-tsconfig-paths
로 추가후, vite.config.ts에서 아래와 같이 수정
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tsconfigPaths from 'vite-tsconfig-paths'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths()],
})
** 틀린 부분은 알려주시면 확인 후 수정할 수 있도록 하겠습니다.
출처 : Vite 이야기 - 테오의 프론트엔드
참고자료 : Prettier 옵션 설명
Eslint -configure/plugins
Eslint -rules
원티드 1주차 강의