Bun with Claude Code
Learn about bun using Claude Code. Practical tips and code examples included.
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!');
Summary
Bunはその圧倒的な速度と統合されたツールチェインで、開発体験を大幅に改善します。Claude Codeと組み合わせれば、Bun特有のAPIやパターンも素早く習得できます。API開発ガイドやテスト戦略も参考にしてください。
Bunの詳細はBun公式ドキュメントを参照してください。
#Claude Code
#Bun
#ランタイム
#JavaScript
#performance
Related Posts
Use Cases
Use Cases
How to Supercharge Your Side Projects with Claude Code [With Examples]
How to Supercharge Your Side Projects with Claude Code [With Examples]. A practical guide with code examples.
Use Cases
Use Cases
How to Automate Refactoring with Claude Code
Learn how to automate refactoring using Claude Code. Includes practical code examples and step-by-step guidance.
Use Cases
Use Cases
Complete CORS Configuration Guide with Claude Code
Learn about complete cors configuration guide using Claude Code. Practical tips and code examples included.