๐Ÿ› /Dev Tool

[Storybook] ๋ฆฌ์•กํŠธ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์‹œ ์œ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ฆฌ ๋„๊ตฌ, ์Šคํ† ๋ฆฌ๋ถ์„ ๋„์ž…ํ•ด๋ณด๊ธฐ - 1

ํ•œ๋‚˜ 2022. 1. 28. 16:29

 

 

๋ฆฌ์•กํŠธ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์‹œ ์œ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ฆฌ ๋„๊ตฌ, ์Šคํ† ๋ฆฌ๋ถ์„ ๋„์ž…ํ•ด๋ณด๊ธฐ

(CRA + ESLint + TypeScript + Storybook + chromatic)

 

 

์˜ˆ์ „ ํ”„๋กœ์ ํŠธ์—์„œ ๋””์ž์ธ์ด ์ˆ˜์‹œ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”๋ฐ, ๋””์ž์ธ ๋ฒ„์ „ ๊ด€๋ฆฌ ์ฒด๊ณ„๋„ ์ œ๋Œ€๋กœ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์•„์„œ ์–ด๋Š ๊ฒƒ์ด ์ตœ์‹  ๋ฒ„์ „์ธ์ง€, ์œ ํšจํ•œ ๋””์ž์ธ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค. Theme์ด ์„ธ ๊ฐ€์ง€๋‚˜ ๋‹ค๋ฅธ ๋””์ž์ธ์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ปดํฌ๋„ŒํŠธ์™€ ํŽ˜์ด์ง€๋ฅผ ๋‹ค๋ฃจ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ๋‚œ๊ฐํ–ˆ๋‹ค. ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•œ๋‹ค๊ณ  ๊ฐ€๋ฉด, ๋„ˆ๋ฌด ๋งŽ์€ prop์ด ๊ฑธ๋ ค์„œ ๋ฒˆ๋ฒˆํžˆ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋„ ์—†๋Š” ์™€์ค‘์— ๋””๋ฒ„๊น… ํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค์—ˆ๋‹ค.

 

 

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋””์ž์ด๋„ˆ์™€ ํ˜‘์—… ํšจ์œจ๋„ ๋†’์ด๊ณ , ์‹œ๊ฐ์ ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋”ฐ๋กœ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ฐœ๋ฐœํ•ด์„œ bottom-up ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ์˜ ์•ˆ์ •์„ฑ์— ์ฃผ๊ณ  ์‹ถ์—ˆ๋‹ค. ๋งˆ์นจ, ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋Š” CI ๋ฅผ ์œ„ํ•œ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์ž‘์„ฑ ์ค‘์ด๊ณ , ๋‚˜๋Š” ๊ทธ๋™์•ˆ ์›น ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ํ”„๋ก ํŠธ์—”๋“œ ๊ธฐ๋ณธ ํ”„๋กœ์ ํŠธ ์„ค์ •์„ ํ…œํ”Œ๋ฆฟํ™”ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์–ด๋–จ๊นŒ ํ•˜๊ณ  ์ด๋Ÿฐ ์ €๋Ÿฐ ํ…Œ์ŠคํŠธ๋ฅผ ์‹œ๋„ํ–ˆ๋‹ค. Storybook์€ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๋ฌธ์ œ์ ๋“ค์„ ์–ด๋Š ์ •๋„๋Š” ๋ณด์ถฉํ•ด์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค. ์ด๋ฒˆ template ํ”„๋กœ์ ํŠธ์—์„œ๋Š” CRA๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์—ฌ๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ์™€ ํ˜‘์—… ์‹œ์— ๊ฐ™์€ ์ฝ”๋“œ ํฌ๋งท์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ESLint/Prettier ์„ค์ •์„ ํ•œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ์˜ ์„ค์ •์€ TypeScript๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํ•œ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ ํ˜‘์—…์„ ์œ„ํ•œ ์ค€๋น„๊ฐ€ ๋œ๋‹ค๋ฉด, Storybook ํ™˜๊ฒฝ์„ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ๋ฐ, ๋‹จ๊ณ„๊ฐ€ ์กฐ๊ธˆ ๋งŽ์œผ๋‹ˆ ์‹œ๋ฆฌ์ฆˆ๋กœ ์ž‘์„ฑํ•  ์˜ˆ์ •์ด๋‹ค. ๋‚˜์ค‘์—๋Š” chromatic์œผ๋กœ ์ •์  ์Šคํ† ๋ฆฌ๋ถ์„ ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

Create React App

Creact React App์„ ํ†ตํ•ด bare project๋ฅผ ๋งŒ๋“ค์–ด์ค„ ๊ฒƒ์ธ๋ฐ, ์—ฌ๊ธฐ์— typescript ํ”Œ๋ž˜๊ทธ๋ฅผ ๋„ฃ์–ด์„œ ์ธ์Šคํ†จํ•œ๋‹ค.

์šฐ์„  ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค.

 

 

๊ทธ๋ฆฌ๊ณ  ํ„ฐ๋ฏธ๋„์— ๊ฐ€์„œ, ์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ๋กœ๋กœ ๋“ค์–ด๊ฐ€์ค€ ๋‹ค์Œ, ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

# ์˜ˆ) npx create-react-app react-typescript-storybook-template --template=typescript

npx create-react-app <ํ”„๋กœ์ ํŠธ์ด๋ฆ„> --template=typescript 

ํ•„์š”ํ•œ ๋””ํŽœ๋˜์‹œ๋“ค์„ ๋‹ค ๋ฐ›๊ณ  ๋‚˜๋ฉด, ์•„๋ž˜์ฒ˜๋Ÿผ ํ„ฐ๋ฏธ๋„ ํ™”๋ฉด์ด ๋ฐ”๋€๋‹ค.

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋กœ ์ง„์ž…ํ•˜๊ณ , ์ฝ”๋“œ ์—๋””ํ„ฐ๋ฅผ ์—ด์–ด์ค€๋‹ค.

์‚ฌ์‹ค ์•„๋ž˜๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ”Œ๋ž˜๊ทธ๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  ์ธ์Šคํ†จํ–ˆ์„ ๋•Œ์˜ ํด๋” ๊ตฌ์กฐ์ด๋‹ค.

๐Ÿ“ฆsrc
 โ”ฃ ๐Ÿ“œApp.css
 โ”ฃ ๐Ÿ“œApp.js
 โ”ฃ ๐Ÿ“œApp.test.js
 โ”ฃ ๐Ÿ“œindex.css
 โ”ฃ ๐Ÿ“œindex.js
 โ”ฃ ๐Ÿ“œlogo.svg
 โ”ฃ ๐Ÿ“œreportWebVitals.js
 โ”— ๐Ÿ“œsetupTests.js

์œ„ ํด๋” ํŠธ๋ฆฌ ๊ตฌ์กฐ์—์„œ ์•„๋ž˜๋งŒ ๋‚จ๊ธฐ๊ณ , ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ js->ts๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  JSX ๋ฌธ๋ฒ•์ด ์žˆ๋Š” ํŒŒ์ผ์˜ ๊ฒฝ์šฐ tsx๋กœ ํ™•์žฅ์ž ๋ช…์„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ํ•œ ๊ฒฝ์šฐ ์ƒ๋žตํ•ด๋„ ๋˜๋Š” ๊ณผ์ •์ด๋‹ค.

๐Ÿ“ฆsrc
 โ”ฃ ๐Ÿ“œApp.test.js
 โ”ฃ ๐Ÿ“œApp.tsx
 โ”ฃ ๐Ÿ“œindex.tsx
 โ”ฃ ๐Ÿ“œreportWebVitals.ts
 โ”— ๐Ÿ“œsetupTests.ts

๊ทธ๋ฆฌ๊ณ  ํ•„์š”ํ•œ ๋””ํŽœ๋˜์‹œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ›์•„์ค€๋‹ค.

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

# or

yarn add typescript @types/node @types/react @types/react-dom @types/jest

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด๊ณณ์—์„œ ๋” ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ์กด CRA ํ”„๋กœ์ ํŠธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

Git initialization

๋‹ค์‹œ ๊นƒํ—™์œผ๋กœ ๋Œ์•„๊ฐ€์„œ, ์ด์ œ ์›๊ฒฉ ์ €์žฅ์†Œ๋ฅผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

echo "# react-typescript-storybook-template" >> README.md  
git init  
git add README.md  
git commit -m "first commit"  
git branch -M main  
git remote add origin [https://github.com/hannah26hannah/react-typescript-storybook-template.git](https://github.com/hannah26hannah/react-typescript-storybook-template.git)  
git push -u origin main

 

TypeScript configuration

๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•ด์„œ ์‹คํ–‰ํ•ด์ฃผ๊ณ , ๋‚˜๋จธ์ง€ ๋ณ€๊ฒฝ๋œ ์†Œ์Šค ์ฝ”๋“œ๋“ค๋„ ์ถ”๊ฐ€ํ•ด์„œ ์ปค๋ฐ‹ํ•ด์ค€๋‹ค.

tsconfig.json ํŒŒ์ผ์ด ์—†์œผ๋‹ˆ ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— echo "" > tsconfig.json ์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€ ๋‹ค์Œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.


{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "noImplicitAny": true, // added
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "outDir": "./dist", // added
    "typeRoots": ["./node_modules/@types", "types"] // added
  },
  "include": ["src"]
}

Next : ์ด์ œ ์ฝ”๋“œ ํฌ๋งทํŒ…์„ ์œ„ํ•ด์„œ ESLint์™€ Prettier๋ฅผ ์ธ์Šคํ†จํ•  ๊ฒƒ์ด๋‹ค. Git์„ ํ†ตํ•ด ์†Œ์Šค๋ฅผ ์˜ฌ๋ฆด ๋•Œ ์ž๋™์œผ๋กœ ESLint์™€ Prettier๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋„์™€์ฃผ๋Š” husky์™€ lint-staged๋„ ๊ฐ™์ด ์ธ์Šคํ†จํ•˜๊ณ  ์„ค์ •ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

 


์—ฌ๊ธฐ๊นŒ์ง€ ์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด, ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. 

 

Full Source Code

 

GitHub - hannah26hannah/react-typescript-storybook-template: React (TypeScript) project with basic Storybook configuration.

React (TypeScript) project with basic Storybook configuration. - GitHub - hannah26hannah/react-typescript-storybook-template: React (TypeScript) project with basic Storybook configuration.

github.com