CI が npm install を bun install に
TL;DR
bun installはロックファイルが あれば npm の 4〜5 倍速い。 うちで 52 秒 → 12 秒 - Bun Shell
( $tagged template)で npm scripts を TypeScript に書き直すと OS 差異を 気に しなくていい - やらかしポイント:
source .envは Bun Shell では動かない
背景:npm scripts の OS 差異問題
ある
{
"scripts": {
"build": "rm -rf dist && tsc"
}
}
rm は Windows の cmd.exe にない。rimraf と cross-env を
Bun Shell とは
Bun v1.0.24$ tagged template literal で
import { $ } from "bun";
await $`rm -rf dist`; // rm -rf が Windows でも動く
await $`mkdir -p dist/assets`;
const output = await $`ls -la`.text(); // stdout を文字列で取得
const pkg = await $`cat package.json`.json(); // JSON を直接パース
Bun v1.0.24 リリースノート によると、bash/sh の
npm scripts を置き換える
before
{
"scripts": {
"clean": "rimraf dist",
"build": "npm run clean && tsc && node scripts/copy.cjs",
"dev": "cross-env NODE_ENV=development tsx watch src/index.ts"
}
}
after(scripts/build.ts)
import { $ } from "bun";
const env = process.env.NODE_ENV ?? "production";
await $`rm -rf dist`;
await $`tsc`;
await $`cp -r src/assets dist/`;
if (env === "production") await $`bun run scripts/minify.ts`;
{ "scripts": { "build": "bun run scripts/build.ts" } }
TypeScript でif でrimraf/cross-env の
やらかし:source .env が動かない
.env を source で
// NG: Bun Shell に source コマンドはない
await $`source .env && tsc`; // → "source: not found"
Bun Shell は bash の.env は bun run 実行時に
// OK: bun run scripts/build.ts だけで .env が展開済み
const dbUrl = process.env.DATABASE_URL;
ドキュメントを
CI の実測値
GitHub Actionsubuntu-latest、
| ステップ | npm | bun |
|---|---|---|
| install | 52 秒 | 12 秒 |
| build | 8 秒 | 6 秒 |
GitHub Actions の
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- run: bun install --frozen-lockfile
- run: bun run build
まとめ
cross-env/rimrafを消して TypeScript で ビルドスクリプトを 書くと OS 差異を 気に しなくていい bun installの速さは CI に 効く。 40 秒の 短縮は 体感でかなり違う sourceは Bun Shell にない。.envは Bun の自動 ロードに 任せる