Skip to content

perf(build): minify bundle and compiled binary#251

Merged
rafa-thayto merged 1 commit into
mainfrom
rafa-thayto/bundle-size
Apr 30, 2026
Merged

perf(build): minify bundle and compiled binary#251
rafa-thayto merged 1 commit into
mainfrom
rafa-thayto/bundle-size

Conversation

@rafa-thayto
Copy link
Copy Markdown
Contributor

Summary

  • Adds --minify to all bun build invocations: the cli.js bundle, the local --compile binary, and the cross-platform release build in scripts/build.ts.
  • Reduces the shipped clerk binary by ~1 MB on every target (63.38 MB → 62.39 MB on darwin-arm64 measured locally).
  • Reduces the bundled cli.js artifact by ~41% (2.37 MB → 1.40 MB).
  • No behavior change. --define-injected values (CLI_VERSION, CLI_ENV_PROFILES) are unaffected because Bun substitutes defines before minification.

Why minify only

Reduction beyond --minify requires changing the deploy model (the embedded Bun runtime is ~60 MB of the binary and is fixed). I evaluated and rejected:

  • --target=bun (the default) — grew the bundle: 1.40 MB → 1.63 MB.
  • --sourcemap=none on --compilegrew the binary by 1.85 MB (Bun quirk; default behavior is smaller).
  • --bytecode — fails on cli.ts's top-level await import() for the completion fast path; bytecode also typically increases output size.
  • Switching @inquirer/prompts to per-package imports — tree-shaking already eliminates unused prompts (verified: 360 KB used vs 420 KB if import * as).

Test plan

  • bun run build produces cli.js of 1,400,498 B (down from 2,372,203 B)
  • bun run build:compile produces dist/clerk of 62,388,832 B (down from 63,379,552 B)
  • bun run scripts/build.ts --target darwin-arm64 --version 0.0.0-test succeeds and the binary reports the injected version
  • bun run typecheck passes
  • bun run lint passes
  • bun run format:check passes
  • bun run test — all 82 unit + integration tests pass
  • Manual smoke on the minified compiled binary: --version, --help, apps --help, init --help, completion bash all behave correctly
  • CI runs bun changeset status --since=origin/main (verified locally with staged changeset: exits 0, "clerk: patch")
  • CI runs bun run test:e2e against the source (not the compiled binary), so minify is not exercised but also not broken

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

🦋 Changeset detected

Latest commit: 6ac3b1b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
clerk Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3a912fe6-9166-4523-8a97-7aca3f7f53a6

📥 Commits

Reviewing files that changed from the base of the PR and between 44524c0 and 6ac3b1b.

📒 Files selected for processing (3)
  • .changeset/bundle-size.md
  • packages/cli-core/package.json
  • scripts/build.ts

📝 Walkthrough

Walkthrough

This change enables build minification across the CLI build pipeline. A changeset metadata file documents the patch release for the clerk package. The build and build:compile scripts in packages/cli-core/package.json add the --minify flag to Bun build invocations. The main build script in scripts/build.ts adds --minify to the Bun compile command for selected targets. These modifications reduce the output bundle sizes without altering functionality.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'perf(build): minify bundle and compiled binary' directly and accurately summarizes the main change—adding minification to build processes for bundle and binary reduction.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, covering the modifications made, their impact, evaluation of alternatives, and detailed test results.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

Adds `--minify` to `bun build` and `bun build --compile` invocations in
`packages/cli-core/package.json` and the cross-platform release builder
in `scripts/build.ts`.

Reduces the shipped `clerk` binary by ~1 MB across all platforms
(63.38 MB → 62.39 MB on darwin-arm64 locally) and the bundled `cli.js`
artifact by ~41% (2.37 MB → 1.40 MB). No behavior change: tests,
typecheck, lint, format and end-to-end binary smoke (`--version`,
`--help`, `apps`, `init --help`, `completion bash`) all pass.

Bun applies `--define` substitutions before minification, so
`CLI_VERSION` and `CLI_ENV_PROFILES` injection continue to work; the
release smoke test (compiled binary `--version`) still matches the
injected version.
@rafa-thayto rafa-thayto force-pushed the rafa-thayto/bundle-size branch from eeda3f0 to 6ac3b1b Compare April 30, 2026 15:09
@rafa-thayto rafa-thayto requested a review from wyattjoh April 30, 2026 17:54
@rafa-thayto rafa-thayto merged commit 215daae into main Apr 30, 2026
10 checks passed
@rafa-thayto rafa-thayto deleted the rafa-thayto/bundle-size branch April 30, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants