HOW-TO GUIDES

create-react-app + typescript + tailwindcss + eslint 한 번에 설정하는 법

encredible 2021. 8. 15. 09:25

아래에 있는 2개의 글의 내용을 따라하지 않고 한 번에 할 수 있도록 실제 파일 값을 알려드립니다. 그대로 따라하시면 됩니다.

https://andrebnassis.medium.com/setting-eslint-on-a-react-typescript-project-2021-1190a43ffba

https://tailwindcss.com/docs/guides/create-react-app

create-react-app을 typescript template으로 생성

npx create-react-app "your app name" --template typescript

package.json 파일 변경

name을 만든 app 이름으로 변경하셔야 합니다.

{
  "name": "Your app name",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/react": "^11.1.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/user-event": "^12.1.10",
    "@craco/craco": "^6.2.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react-dom": "^17.0.9",
    "@types/react": "^17.0.17",
    "@typescript-eslint/eslint-plugin": "^4.29.1",
    "@typescript-eslint/parser": "^4.29.1",
    "typescript": "^4.1.2",
    "autoprefixer": "^9.8.6",
    "eslint": "^7.32.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-import-resolver-typescript": "^2.4.0",
    "eslint-plugin-import": "^2.24.0",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.24.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "postcss": "^7.0.36",
    "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.7"
  }
}

.eslintrc.js 파일 추가

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'plugin:react/recommended',
    'airbnb',
    'plugin:@typescript-eslint/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: [
    'react',
    '@typescript-eslint',
    'react-hooks',
  ],
  rules: {
    'no-use-before-define': 'off',
    '@typescript-eslint/no-use-before-define': ['error'],
    'react/jsx-filename-extension': ['warn', { extensions: ['.tsx'] }],
    'import/extensions': [
      'error',
      'ignorePackages',
      {
        ts: 'never',
        tsx: 'never',
      },
    ],
    'no-shadow': 'off',
    '@typescript-eslint/no-shadow': ['error'],
    '@typescript-eslint/explicit-function-return-type': [
      'error',
      {
        allowExpressions: true,
      },
    ],
    'max-len': ['warn', { code: 120 }],
    'react-hooks/rules-of-hooks': 'error',
    'react-hooks/exhaustive-deps': 'warn',
    'import/prefer-default-export': 'off',
    'react/prop-types': 'off',
  },
  settings: {
    'import/resolver': {
      typescript: {},
    },
  },
};

.eslintignore 파일 추가

*.css
*.svg

craco.config.js 파일 추가

module.exports = {
  style: {
    postcss: {
      plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
      ],
    },
  },
};

tailwind.config.js 파일 추가

module.exports = {
  purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
};

src/index.css 라인 추가

@tailwind base;
@tailwind components;
@tailwind utilities;

yarn 설치

yarn install

eslint 확인

npx eslint src/* --fix

/Users/jaegwan/PycharmProjects/test/src/App.css
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override

/Users/jaegwan/PycharmProjects/test/src/App.tsx
5:1 error Missing return type on function @typescript-eslint/explicit-function-return-type
5:1 warning Missing return type on function @typescript-eslint/explicit-module-boundary-types

/Users/jaegwan/PycharmProjects/test/src/index.css
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override

/Users/jaegwan/PycharmProjects/test/src/logo.svg
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override

/Users/jaegwan/PycharmProjects/test/src/reportWebVitals.ts
3:25 error Missing return type on function @typescript-eslint/explicit-function-return-type
3:25 warning Missing return type on function @typescript-eslint/explicit-module-boundary-types

여기서 error 2개를 수정해주시면 됩니다. App.tsx로 가서 function App(): JSX.Element { 로 변경 하시면 되고, reportWebVitals.ts로 가셔서 const reportWebVitals = (onPerfEntry?: ReportHandler): void => { 로 변경하시면 됩니다.

yarn 실행

yarn start

이제 정상적으로 실행되는 걸 확인하실 수 있습니다. 내용이 길어 보이지만 순서대로만 잘 따라하시면 5분 내로 typescript react 에서 eslint와 tailwindcss 를 설정하실 수 있습니다.