Use Cases

Bun: Claude Code 활용 가이드

bun: Claude Code 활용. 실용적인 팁과 코드 예시를 포함합니다.

Bunランタイムの활용をClaude Code로 가속화하기

BunはJavaScript/TypeScriptのオールインワンランタイムです。패키지マネージャー、バンドラー、테스트ランナーを内蔵し、Node.jsの数倍の速度で動作します。Claude Codeを활용して、Bunの強力な機能を効率よく도입합시다。

프로젝트の立ち上げ

> Bunで新しいWeb앱프로젝트를 생성해줘。
> TypeScript、Hono프레임워크、테스트설정を含めて。
bun init my-app
cd my-app
bun add hono
// src/index.ts
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { logger } from 'hono/logger';

const app = new Hono();

app.use('*', logger());
app.use('/api/*', cors());

app.get('/api/health', (c) => {
  return c.json({ status: 'ok', runtime: 'bun' });
});

app.get('/api/users/:id', async (c) => {
  const id = c.req.param('id');
  const user = await getUser(id);
  return c.json(user);
});

export default {
  port: 3000,
  fetch: app.fetch,
};

BunのビルトインAPIを활용

> Bunの파일操作APIとSQLiteを使って、
> シンプルな데이터ストア를 구현해줘。
// src/db.ts
import { Database } from 'bun:sqlite';

const db = new Database('app.db', { create: true });

db.run(`
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  )
`);

export const createUser = db.prepare(
  'INSERT INTO users (name, email) VALUES ($name, $email)'
);

export const getUser = db.prepare(
  'SELECT * FROM users WHERE id = $id'
);

// Bunの파일操作
export async function saveUpload(file: File) {
  const path = `./uploads/${file.name}`;
  await Bun.write(path, file);
  return path;
}

export async function readConfig() {
  const file = Bun.file('./config.json');
  return await file.json();
}

테스트の実行

Bunには高速な테스트ランナーが内蔵されています。

> API엔드포인트の테스트を書いて。
> Bunの테스트ランナーを使って。
// src/index.test.ts
import { describe, expect, it, beforeAll, afterAll } from 'bun:test';

describe('API endpoints', () => {
  it('GET /api/health returns ok', async () => {
    const res = await fetch('http://localhost:3000/api/health');
    const data = await res.json();
    expect(res.status).toBe(200);
    expect(data.status).toBe('ok');
  });

  it('GET /api/users/:id returns user', async () => {
    const res = await fetch('http://localhost:3000/api/users/1');
    expect(res.status).toBe(200);
    const user = await res.json();
    expect(user).toHaveProperty('name');
  });
});
# テスト実行
bun test

# ウォッチモード
bun test --watch

# カバレッジ
bun test --coverage

번들と빌드

> 프론트엔드のコードをBunで번들して。
> Tree-shakingとminifyも유효にして。
// build.ts
await Bun.build({
  entrypoints: ['./src/client/index.tsx'],
  outdir: './dist',
  target: 'browser',
  minify: true,
  splitting: true,
  sourcemap: 'external',
  define: {
    'process.env.NODE_ENV': '"production"',
  },
});

console.log('Build complete!');

정리

Bunは그圧倒的な速度と통합されたツールチェインで、개발体験を大幅に改善します。Claude Code와組み合わせれば、Bun特有のAPIやパターンも빠르게習得할 수 있습니다。API개발가이드테스트戦略도 참고하세요.

Bun의 상세 정보는Bun공식 문서를 참고하세요.

#Claude Code #Bun #ランタイム #JavaScript #performance