Use Cases

Claude CodeでBunランタイムを活用した高速開発

Claude Codeを使ってBunランタイムでの開発を効率化する方法を解説。パッケージ管理、テスト、バンドル、HTTPサーバーまでBunの全機能を実践的に紹介します。

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 #パフォーマンス