🐀 [F/E] React ν”„λ‘œμ νŠΈ 초기 Setting

leehyunjuΒ·2023λ…„ 1μ›” 30일
9

(1) React μ‹€ν–‰ μ „ 단계

κ°€μž₯ λ¨Όμ € Reactλ₯Ό μ‹€ν–‰ν•˜κΈ° μœ„ν•΄μ„œλŠ” μ•„λž˜ ν•­λͺ©λ“€μ΄ ν•„μš”ν•©λ‹ˆλ‹€.

node μ„€μΉ˜ // package manageλ₯Ό μ‚¬μš© ν•˜κΈ° μœ„ν•΄μ„œ μ„€μΉ˜ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€.

// μ„€μΉ˜ λλŠ”μ§€ ν™•μΈν•˜κΈ° μœ„ν•΄ ν„°λ―Έλ„μ—μ„œ μ•„λž˜ λͺ…λ Ήμ–΄ μž…λ ₯
$node -v 

npm μ„€μΉ˜

$npm i // μ„€μΉ˜ λͺ…λ Ήμ–΄
$npm -v // 버전 확인 λͺ…λ Ήμ–΄

(2) React μ‹€ν–‰ν•˜κΈ°

TypeScript μ‚¬μš©ν•˜λ €λ©΄ Create TypeScript project μ²΄ν¬ν•˜κ³  μƒμ„±ν•˜λ©΄ λ©λ‹ˆλ‹€.

(3) React μž‘μ—… ν™˜κ²½μ„ΈνŒ…

  • Webpack
  • Prettier
  • ESLint
  • Babel

(3-1) Webpack

  • 파일 λ‹¨μœ„μ˜ μžλ°”μŠ€ν¬λ¦½νŠΈ λͺ¨λ“ˆ 관리 (import, export)
  • μ›Ή 개발 μž‘μ—… μžλ™ν™” 도ꡬ (HTML, CSS, JS, images) β†’ μ†ŒμŠ€μ½”λ“œ λ‚œλ…ν™”
  • μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λΉ λ₯Έ λ‘œλ”© 속도와 높은 μ„±λŠ₯

(3-2) Prettier

μ—¬λŸ¬ μ‚¬λžŒμ΄ μž‘μ—…μ„ 해도 ν•œ μ‚¬λžŒμ΄ μž‘μ—…ν•œ 것 처럼 일관적인 μ½”λ“œ μŠ€νƒ€μΌμ„ μœ μ§€ν•  수 있게 λ„μ™€μ£ΌλŠ” νˆ΄μ΄λ‹€.

쀄 λ°”κΏˆ, 곡백, λ“€μ—¬ μ“°κΈ° 같은 μŠ€νƒ€μΌμ„ ꡐ정해 μ€€λ‹€.

🐣 κΈ°λ³Έ μ„€μ • μ½”λ“œ

{
    "bracketSpacing": true,
    "printWidth": 140,
    "singleQuote": true,
    "trailingComma": "none",
    "tabWidth": 4,
    "useTabs": false
}

더 λ§Žμ€ 속성을 μž‘μ•„μ£Όκ³  μ‹Άλ‹€λ©΄ πŸ‘‡
https://prettier.io/docs/en/index.html

(3-3) ESLint

문법 μ—λŸ¬λ₯Ό μž‘μ•„μ£Όκ±°λ‚˜ 더 쒋은 μ½”λ“œ κ΅¬ν˜„ 방식을 μ‚¬μš©ν•˜λ„λ‘ ν•΄μ€€λ‹€.

.eslintrc λΌλŠ” νŒŒμΌμ„ μƒμ„±ν•΄μ„œ μ‚¬μš©ν•œλ‹€.

  • Monorepo와 같이 ν•˜λ‚˜μ˜ μ½”λ“œ μ €μž₯μ†Œμ—μ„œ μ—¬λŸ¬ 개의 ν”„λ‘œμ νŠΈκ°€ κ³΅μ‘΄ν•˜λŠ” 경우 맀우 유용
  • 기본으둜 μ œκ³΅λ˜λŠ” κ·œμΉ™ 외에도 좔가적인 κ·œμΉ™ μ‚¬μš© κ°€λŠ₯
  • λ‹€μ–‘ν•œ ν”ŒλŸ¬κ·ΈμΈλ„ μ‚¬μš© ν•  수 μžˆλ‹€.
  • 기본적인 μ½”λ“œ ν™˜κ²½ 섀정도 κ°€λŠ₯ν•˜κ³  μ—λŸ¬ 문법 듀도 μž‘μ•„μ€Œ

πŸ‘‰ eslintrc setting code

{
    "root": true,
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": ["prettier", "plugin:react/jsx-runtime", "plugin:jsx-a11y/recommended", "plugin:react-hooks/recommended"],
    "settings": {
        "import/resolver": {
            "node": {
                "moduleDirectory": ["node_modules", "src/"]
            }
        }
    },
    "parser": "@babel/eslint-parser",
    "parserOptions": {
        "ecmaFeatures": {
            "experimentalObjectRestSpread": true,
            "impliedStrict": true
        },
        "ecmaVersion": 12
    },
    "plugins": ["prettier", "react", "react-hooks"],
    "rules": {
        "react/jsx-filename-extension": 0,
        "no-param-reassign": 0,
        "react/prop-types": 1,
        "react/require-default-props": 0,
        "react/no-array-index-key": 0,
        "react/jsx-props-no-spreading": 0,
        "react/forbid-prop-types": 0,
        "import/order": 0,
        "no-console": 0,
        "jsx-a11y/anchor-is-valid": 0,
        "prefer-destructuring": 0,
        "no-shadow": 0,
        "no-unused-vars": [
            1,
            {
                "ignoreRestSiblings": false
            }
        ],
        "prettier/prettier": 0  //prettier 에 λŒ€ν•œ κ²½κ³  끄기
    }
}

(3-4) Babel

λ¨Όμ € 바벨을 μ„€λͺ…ν•˜κΈ° μ „, 폴리필에 λŒ€ν•΄μ„œ κ°„λ‹¨ν•˜κ²Œ μ•Œμ•„λ΄…μ‹œλ‹€.

πŸ’Ÿ Polyfill ?

폴리필은 κ°œλ°œμžκ°€ νŠΉμ • κΈ°λŠ₯이 μ§€μ›λ˜μ§€ μ•ŠλŠ” λΈŒλΌμš°μ €λ₯Ό μœ„ν•΄ μ‚¬μš©ν•  수 μžˆλŠ” μ½”λ“œ μ‘°κ°μ΄λ‚˜ ν”ŒλŸ¬κ·ΈμΈμ΄λ‹€. λΈŒλΌμš°μ €μ—μ„œ μ§€μ›ν•˜μ§€ μ•ŠλŠ” κΈ°λŠ₯듀에 λŒ€ν•œ ν˜Έν™˜μ„±μ„ μ±„μ›Œ λ„£λŠ”λ‹€κ³  ν•˜μ—¬ 폴리필 이라고 μΉ­ν•©λ‹ˆλ‹€.

πŸ’Ÿ babel-polyfill

바벨은 μ΄λŸ¬ν•œ 폴리필을 μ†μ‰½κ²Œ μ§€μ›ν•˜κΈ° μœ„ν•΄ babel-polyfill κΈ°λŠ₯을 μ§€μ›ν•©λ‹ˆλ‹€. 즉, 바벨 폴리필은 μ΅œμ‹  문법을 써도 이전 버전 λΈŒλΌμš°μ € ν™˜κ²½μ—μ„œλ„ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ΅œμ‹  문법을 νŠΉμ • μ˜ˆμ „ λ²„μ „μ˜ JS둜 λ³€ν™˜ν•΄μ£ΌλŠ” 툴이라고 μƒκ°ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€.

// Babel Input: ES2015 arrow function
[1, 2, 3].map((n) => n + 1);

// Babel Output: ES5 equivalent
[1, 2, 3].map(function(n) {
  return n + 1;
});]

κ·Έ μ™Έ

react-hot-loader μ„€μΉ˜ν•˜κΈ° *일λͺ… ν•«λ¦¬λ‘œλ“œ 라고 ν•œλ‹€.

  • μŠ€νƒ€μΌ λ˜λŠ” JSXλ₯Ό λ³€κ²½ μ¦‰μ‹œ μ›Ή λΈŒλΌμš°μ €μ—μ„œ μžλ™ λ°˜μ˜λœλ‹€.
  • next.js μ—μ„œλŠ” μ œκ³΅ν•΄μ£ΌλŠ” νˆ΄μ΄μ§€λ§Œ CRA μ—μ„œλŠ” μ„€μ •ν•΄μ£Όμ–΄μ•Ό ν•œλ‹€.

μ„ΈνŒ… 방법

// μ„€μΉ˜ λͺ…λ Ήμ–΄
$npm i react-hot-loader

// 적용 μ†ŒμŠ€ μ½”λ“œ
import { hot } from 'react-hot-loader';

const App = () => {
    return (
        <Router>
            <Routes>
                <Route path="/" element={<Board1 />} />
                <Route path="/board2" element={<Board2 />} />
            </Routes>
        </Router>
    );
};

export default hot(module)(App);
profile
μ•„λŠ‘ν•œ λ‡Œκ³΅κ°„ 🧠

0개의 λŒ“κΈ€