Preact

소개

설치

bun add preact

설정

tsconfig.json 수정

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  }
}

index.html

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sixtick</title>
</head>
<body>
    <div id="app"></div>
    <script type="module" src="/dist/index.js"></script>
    <div>hello</div>
</body>
</html>

index.tsx

import { render } from 'preact';
import { useState } from 'preact/hooks';

function App() {
  const [count, setCount] = useState(0);

  return (
    <div style={{ padding: '20px', fontFamily: 'sans-serif' }}>
      <h1>Bun + Preact 프로젝트</h1>
      <p>현재 카운트: {count}</p>
      <button onClick={() => setCount(count + 1)}>증가</button>
    </div>
  );
}

// 브라우저 환경에 렌더링 (HTML에 id가 'app'인 엘리먼트가 있다고 가정)
const appElement = document.getElementById('app');
if (appElement) {
  render(<App />, appElement);
}

페이지 서빙

tsx 를 js로 변환해야한다. 서버에 빌드 코드를 삽입하던지 수동으로 bun build ./src/page/index.tsx --outdir ./dist --minify 빌드 해줘야 한다.

// serve 에 적용 코드
import { join } from "path";

const IS_DEV = process.env.NODE_ENV !== "production";
const CLIENT_ENTRY = join(process.cwd(), "src/page/index.tsx");
const DIST_DIR = join(process.cwd(), "dist");

// [핵심] 서버 구동 시 처음에 딱 한 번만 미리 빌드해 둠
async function buildClient() {
  await Bun.build({
    entrypoints: [CLIENT_ENTRY],
    outdir: DIST_DIR,
    minify: !IS_DEV, // 프로덕션 환경에서만 압축
  });
  console.log("📦 클라이언트 빌드 완료!");
}

await buildClient();

// 나머지 serve 코드