こんなTypeScriptのソースがあるとします。
cli.ts
console.log("Hello world!");
これを実行ファイルに変換します。
Bunを使う場合
bun build ./cli.ts --compile --outfile cli-bun
Windowsの場合はcli-bun.exeが生成されます。
クロスコンパイルもできます。
# Mac向けバイナリを生成
bun build ./cli.ts --compile --target=bun-darwin-arm64 --outfile cli-bun
Denoを使う場合
deno compile --output cli-deno cli.ts
Denoもクロスコンパイルできます。
deno compile --target aarch64-apple-darwin --output cli-deno cli.ts
ファイルサイズを比較
Hello world!を出力するだけですが、Bun版が約100MB、Deno版が約80MBです。
npxで使用するMCPサーバーをシングルバイナリ化する
最近はリモートMCPが主流ですが、たまにあるnpxでインストールして使う系のMCPサーバーをシングルバイナリにします。Bunで試しました。
お題としてSequential Thinking MCP Serverを取り上げます。
まずbun iで取得します。
bun i @modelcontextprotocol/server-sequential-thinking
node_modules/@modelcontextprotocol/server-sequential-thinking/package.jsonのbinを確認するとこうなっています。
"bin": {
"mcp-server-sequential-thinking": "dist/index.js"
},
実態はdist/index.jsということですね。
なので、このファイルを先程のように
bun build ./node_modules/@modelcontextprotocol/server-sequential-thinking/dist/index.js --compile --outfile sequential-thinking
sequential-thinking.exeが生成されます。
非開発者のPCにNodeが入ってなくてもシングルバイナリにして配布が捗りますね!
参考


