next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Add `bun:bundle` as an external Bun module

Open Jarred-Sumner opened this issue 2 weeks ago • 1 comments

What?

Add bun:bundle to the list of Bun built-in modules that are treated as externals during bundling, alongside existing modules like bun:ffi, bun:jsc, bun:sqlite, etc.

Why?

bun:bundle is a new Bun built-in module that provides compile-time feature flags for dead-code elimination. It allows conditionally including or excluding code paths at bundle time:

import { feature } from "bun:bundle";

if (feature("PREMIUM")) {
  // Only included when PREMIUM flag is enabled
  initPremiumFeatures();
}

The feature() function is replaced with true or false at bundle time. Combined with minification, unreachable code is eliminated entirely.

Use cases:

  • Platform-specific code (feature("SERVER") vs feature("CLIENT"))
  • Environment-based features (feature("DEVELOPMENT"))
  • Gradual feature rollouts / A/B testing
  • Paid tier features

Like other bun:* modules, this needs to be treated as an external to prevent Next.js from attempting to bundle it.

See: https://github.com/oven-sh/bun/pull/25462

How?

Added bun:bundle to:

  • BUN_EXTERNALS in crates/next-taskless/src/constants.rs
  • bunExternals in packages/next/src/build/webpack-config.ts

Updated all related tests:

  • E2E tests in test/e2e/app-dir/bun-externals/
  • Integration tests in test/integration/webpack-bun-externals/
  • Turbopack snapshot tests (regenerated via UPDATE=1 cargo nextest run)

Test Plan

  • [x] Turbopack snapshot test passes: cargo nextest run -E 'test(bun_protocol_external)'
  • [x] All existing bun-externals tests updated to include bun:bundle

Jarred-Sumner avatar Dec 11 '25 02:12 Jarred-Sumner

Allow CI Workflow Run

  • [ ] approve CI run for commit: 1adf81b621ae32335c6aedbdebefa42a31bd5fdc

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

nextjs-bot avatar Dec 11 '25 02:12 nextjs-bot