Web (React)์ Path Mapping
Sonamu Web ํ๋ก์ ํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก@/* ๊ฒฝ๋ก ๋งคํ์ ์ ๊ณตํฉ๋๋ค.
tsconfig.json ์ค์
web/tsconfig.json
Vite ์ค์
TypeScript ์ค์ ๋ง์ผ๋ก๋ ๋ถ์กฑํ๊ณ , Vite์๋ ๋์ผํ alias๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.web/vite.config.ts
์ฌ์ฉ ์์
- Before (์๋ ๊ฒฝ๋ก)
- After (์ ๋ ๊ฒฝ๋ก)
src/pages/users/UserDetail.tsx
- ํ์ผ ์์น๊ฐ ๋ฐ๋๋ฉด ๋ชจ๋ import ์์ ํ์
../../../๊ฐ์ ๋ณต์กํ ๊ฒฝ๋ก- ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง
๋๋ ํ ๋ฆฌ๋ณ ๋งคํ
๋ ๋ง์ ๊ฒฝ๋ก๋ฅผ ๋งคํํ ์ ์์ต๋๋ค.web/tsconfig.json
web/vite.config.ts
API ์๋ฒ์ ๋ชจ๋ ํด์
API ์๋ฒ๋ ์ผ๋ฐ์ ์ผ๋ก path mapping์ ์ฌ์ฉํ์ง ์์ต๋๋ค.์ด์
api/tsconfig.json
- API๋ Vite๋ก ๋น๋๋๋ฏ๋ก ๋ณ๋์ path mapping ๋ถํ์
sonamu๊ฐ์ ํจํค์ง๋ Vite๊ฐ ์๋์ผ๋ก ํด์- ์๋ ๊ฒฝ๋ก๊ฐ ๋ ๋ช ํํ๊ณ ๋จ์ํจ
API ๊ตฌ์กฐ
API๋ ์๋์ ์ผ๋ก ๊ฐ๋จํ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋๋ค.api/src/application/controllers/UserController.ts
API์์ path mapping์ด ํ์ํ๋ค๋ฉด ์ถ๊ฐํ ์ ์์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ๋ถํ์ํฉ๋๋ค.
Sonamu ํจํค์ง Import
Sonamu ์์ฒด๋ path mapping ์์ด importํ ์ ์์ต๋๋ค.sonamu- ๋ฉ์ธ ํจํค์งsonamu/storage- ์คํ ๋ฆฌ์ง ๋๋ผ์ด๋ฒsonamu/cache- ์บ์ ๋๋ผ์ด๋ฒ
Sonamu๋
package.json์ exports ํ๋๋ก ์๋ธํจํค์ง๋ฅผ ์ ๊ณตํฉ๋๋ค.ํ์ Import
ํ์ ๋ง importํ ๋๋type ํค์๋๋ฅผ ์ฌ์ฉํ์ธ์.
IDE ์๋ ์์ฑ
Path mapping์ ์ค์ ํ๋ฉด IDE๊ฐ ์๋์ผ๋ก ์ธ์ํฉ๋๋ค.VS Code
.vscode/settings.json
IntelliJ / WebStorm
์๋์ผ๋กtsconfig.json์ ์ฝ์ด path mapping์ ์ธ์ํฉ๋๋ค.
์๋ ์์ฑ์ด ์๋ํ์ง ์์ ๋
์๋ ์์ฑ์ด ์๋ํ์ง ์์ ๋
์ฆ์:
@/...๊ฒฝ๋ก๊ฐ ๋นจ๊ฐ ๋ฐ์ค- ์๋ ์์ฑ์ด ์๋ํ์ง ์์
-
TypeScript ์๋ฒ ์ฌ์์
-
tsconfig.json ํ์ธ
-
Vite ๊ฐ๋ฐ ์๋ฒ ์ฌ์์
-
node_modules ์ฌ์ค์น
Jest ํ ์คํธ ์ค์
ํ ์คํธ์์๋ path mapping์ ์ฌ์ฉํ๋ ค๋ฉด Jest ์ค์ ์ด ํ์ํฉ๋๋ค.web/jest.config.ts
src/components/Button.test.tsx
์ผ๋ฐ์ ์ธ ๋ฌธ์ ํด๊ฒฐ
Cannot find module '@/...' ์๋ฌ
Cannot find module '@/...' ์๋ฌ
์ฆ์:์์ธ:2. vite.config.ts ํ์ธ3. ํ์ผ ์กด์ฌ ํ์ธ
baseUrl์ด ์ค์ ๋์ง ์์- Vite alias๊ฐ ์ค์ ๋์ง ์์
- ๊ฒฝ๋ก ์คํ
๋น๋๋ ๋์ง๋ง ํ์
์ฒดํฌ ์คํจ
๋น๋๋ ๋์ง๋ง ํ์ ์ฒดํฌ ์คํจ
์ฆ์:์์ธ:
- Vite๋ alias๋ฅผ ์ธ์ํ์ง๋ง TypeScript๋ ์ธ์ํ์ง ๋ชปํจ
tsconfig.json์ baseUrl๊ณผ paths ์ถ๊ฐ:ํ์
์ฒดํฌ๋ ๋์ง๋ง ๋น๋ ์คํจ
ํ์ ์ฒดํฌ๋ ๋์ง๋ง ๋น๋ ์คํจ
์ฆ์:์์ธ:
- TypeScript๋ alias๋ฅผ ์ธ์ํ์ง๋ง Vite๋ ์ธ์ํ์ง ๋ชปํจ
vite.config.ts์ alias ์ถ๊ฐ:๊ถ์ฅ ์ฌํญ
- Web
- API
- ์ผ๊ด์ฑ
๊ถ์ฅ ์ค์ :์ฌ์ฉ ๋ฐฉ๋ฒ:
@/components/*- ์ปดํฌ๋ํธ@/pages/*- ํ์ด์ง@/utils/*- ์ ํธ๋ฆฌํฐ@/hooks/*- ์ปค์คํ ํ@/types/*- ํ์ ์ ์
- ๋๋ฌด ๋ง์ alias (ํผ๋ ๊ฐ์ค)
- ํ์ ๋๋ ํ ๋ฆฌ๋ง๋ค alias (๋ถํ์)
๋ค์ ๋จ๊ณ
tsconfig.json
์ ์ฒด TypeScript ์ค์ ์ ํ์ธํ์ธ์
ํ์ ์ฒดํฌ
ํ์
์ฒดํฌ ์ต์
์ ํ์ตํ์ธ์
์ปดํฌ๋ํธ ์์ฑ
React ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ์ธ์
ํ๋ก์ ํธ ๊ตฌ์กฐ
์ ์ฒด ํ๋ก์ ํธ ๊ตฌ์กฐ๋ฅผ ํ์ธํ์ธ์