๐ Webpack๊ณผ TypeScript๋ก ๋ง๋๋ ์น์ฑ ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ ๊ฐ๋ฐ ์๋ฆฌ์ฆ - ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ ๊ตฌ์กฐ ์ค๊ณ – ๋ ์ง ๋ ๋๋ง์ ์ํ UI ๊ธฐ๋ณธ ๊ตฌ์ฑ
๐ Webpack๊ณผ TypeScript๋ก ๋ง๋๋ ์น์ฑ ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ ๊ฐ๋ฐ ์๋ฆฌ์ฆ
โ 4ํธ: ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ ๊ตฌ์กฐ ์ค๊ณ – ๋ ์ง ๋ ๋๋ง์ ์ํ UI ๊ธฐ๋ณธ ๊ตฌ์ฑ
์ด๋ฒ ํธ์์๋ ๋๋์ด ๋ณธ๊ฒฉ์ ์ผ๋ก ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ์ UI ๊ตฌ์กฐ๋ฅผ ์ค๊ณํ๊ณ ,
๊ธฐ๋ณธ ๋ ์ง ๋ ๋๋ง ๊ธฐ๋ฅ์ ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ๋ฆฌ๋ ์ด๋ฏธ TypeScript ๊ธฐ๋ฐ์ ๊ฐ๋ฐ ํ๊ฒฝ๊ณผ Webpack ์ค์ ์ ์๋ฃํ์ผ๋ฏ๋ก,
์ด๋ฒ ๊ธ๋ถํฐ๋ **“ํ๋ฉด์ ๋ฌ๋ ฅ์ ์ถ๋ ฅํ๋ ์ค์ง์ ์ธ ๊ธฐ๋ฅ ๊ตฌํ”**์ ์ง์คํ ์ ์์ต๋๋ค.
๐ฏ ๋ชฉํ
- ํ ๋ฌ ๋ฌ๋ ฅ ๊ตฌ์กฐ๋ฅผ ์ค๊ณํ๊ณ ๋ ๋๋งํ๋ ํจ์ ๋ง๋ค๊ธฐ
- Date, TypeScript, DOM ์กฐ์์ ํ์ฉํด UI ๊ตฌ์ฑ
- ์บ๋ฆฐ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ๊ทธ๋ฆด ์ ์๋ ์ต์ ๊ธฐ๋ฅ ์์ฑ
๐ง ์บ๋ฆฐ๋ ๊ธฐ๋ณธ ๊ตฌ์กฐ ์ดํดํ๊ธฐ
โ ํ์ํ ๊ตฌ์ฑ ์์
์์ ์ค๋ช
์ฐ/์ ํค๋ | ํ์ฌ ํ์ ์ค์ธ ๋ ์ง |
์์ผ ํค๋ | ์ผ |
๋ ์ง ์ | ํ ๋ฌ ๊ฐ์ ๋ ์ง๋ฅผ ๊ทธ๋ฆฌ๋ ํํ๋ก ํ์ |
์ด์ /๋ค์ ๋ฌ ์ด๋ | ์ ์ ํ ๊ธฐ๋ฅ |
๐ฆ 1. ํด๋ ๋ฐ ํ์ผ ๊ตฌ์กฐ ํ์ฅ
src/
โโโ components/
โ โโโ Calendar.ts
โโโ types.ts
โโโ index.ts
โ๏ธ 2. ๋ฌ๋ ฅ ๋ ๋๋ง์ ์ํ ์ ํธ ํจ์ ๋ง๋ค๊ธฐ
โ getCalendarMatrix – ๋ ์ง ๊ทธ๋ฆฌ๋ ์์ฑ
// src/components/Calendar.ts
export function getCalendarMatrix(year: number, month: number): (Date | null)[][] {
const matrix: (Date | null)[][] = [];
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const startWeekDay = firstDay.getDay(); // ์ผ:0, ์:1, ..., ํ :6
let current = new Date(year, month, 1 - startWeekDay);
for (let week = 0; week < 6; week++) {
const weekRow: (Date | null)[] = [];
for (let day = 0; day < 7; day++) {
weekRow.push(new Date(current));
current.setDate(current.getDate() + 1);
}
matrix.push(weekRow);
}
return matrix;
}
6์ฃผ(์ต๋) * 7์ผ = 42์นธ์ ๊ทธ๋ฆฌ๋ ๋ฌ๋ ฅ์ ์์ฑํฉ๋๋ค.
์ด์ ๋ฌ, ๋ค์ ๋ฌ์ ๋ ์ง๋ ํฌํจํ์ฌ ๋ ์ง ์์น๋ฅผ ๋ง์ถฅ๋๋ค.
๐ผ๏ธ 3. ์บ๋ฆฐ๋ UI DOM ๊ตฌ์ฑํ๊ธฐ
โ renderCalendar ํจ์ ๋ง๋ค๊ธฐ
// src/components/Calendar.ts
export function renderCalendar(target: HTMLElement, year: number, month: number): void {
const matrix = getCalendarMatrix(year, month);
target.innerHTML = `
<div class="calendar-header">
<h2>${year}๋
${month + 1}์</h2>
</div>
<div class="calendar-grid">
${["์ผ", "์", "ํ", "์", "๋ชฉ", "๊ธ", "ํ "]
.map((d) => `<div class="calendar-cell header">${d}</div>`)
.join("")}
${matrix
.flat()
.map((date) => {
const day = date?.getDate();
const inMonth = date?.getMonth() === month;
return `<div class="calendar-cell${inMonth ? "" : " out"}">${day}</div>`;
})
.join("")}
</div>
`;
}
๐งฉ 4. ์บ๋ฆฐ๋ ์ด๊ธฐํ ๋ฐ ์คํ
โ index.ts
import { renderCalendar } from "./components/Calendar";
const root = document.getElementById("calendar-root");
if (root) {
const today = new Date();
renderCalendar(root, today.getFullYear(), today.getMonth());
}
๐จ 5. CSS ์คํ์ผ ์์
โ dist/style.css
.calendar-header {
text-align: center;
margin-bottom: 1rem;
}
.calendar-grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 4px;
}
.calendar-cell {
border: 1px solid #ccc;
padding: 10px;
text-align: center;
min-height: 40px;
}
.calendar-cell.header {
font-weight: bold;
background: #f0f0f0;
}
.calendar-cell.out {
color: #999;
}
โ HTML์ ์ฐ๊ฒฐ
<link rel="stylesheet" href="style.css">
โ ๊ฒฐ๊ณผ ํ์ธ
npm run dev
http://localhost:8080 ์ ์ ์, ํ ๋ฌ์น ๋ฌ๋ ฅ์ด ๊ทธ๋ฆฌ๋ ํํ๋ก ์ ๋ ๋๋ง๋๋ฉด ์ฑ๊ณต!
๐ ์ฐธ๊ณ ์๋ฃ
๋ค์ ํธ ์๊ณ :
๐ 5ํธ – ์ฌ์ฉ์ ์ด๋ฒคํธ ์ถ๊ฐ: ๋ ์ง ์ ํ, ์ ์ด๋ ๊ธฐ๋ฅ ๊ตฌํ
ํ๋ฉด์ ๋ฌ๋ ฅ์ ๊ทธ๋ ธ๋ค๋ฉด, ์ด์ ๋ **“๋์ํ๋ ๋ฌ๋ ฅ”**์ ๋ง๋ค์ด์ผ์ฃ .
์ด๋ฒคํธ ์ฒ๋ฆฌ, DOM ๋ฐ์ธ๋ฉ ๋ฑ ์ค์ ์ผ๋ก ๋ค์ด๊ฐ๋๋ค! ๐