The Bake Development Server is Bun's full-stack development environment. It integrates an incremental bundler, a framework-aware router, and a Hot Module Replacement (HMR) runtime to provide near-instant feedback during development. Unlike traditional dev servers that re-scan or re-bundle large portions of the dependency graph, Bake utilizes a data-oriented incremental graph to ensure that editing a file only triggers a re-bundle of that specific file [src/runtime/bake/DevServer.rs:4-9].
The DevServer is the central orchestrator, typically attached to a Bun.serve instance [src/runtime/bake/DevServer.rs:1-2]. It manages distinct transpilers for the client, server, and SSR environments to ensure correct module resolution and transformation across the full stack [src/runtime/bake/DevServer.rs:129-157].
[src/runtime/bake/DevServer.rs:78].OpaqueFileId identifiers [src/runtime/bake/DevServer.rs:39].[src/runtime/bake/DevServer.rs:160].[src/runtime/bake/DevServer.rs:50].[src/runtime/bake/DevServer.rs:162].The following diagram illustrates the flow from a filesystem event to a client-side update.
DevServer Update Pipeline
Sources: [src/runtime/bake/DevServer.rs:32-50], [src/runtime/bake/DevServer.rs:129-157], [test/cli/hot/hot.test.ts:132-144]
Bake uses BundleV2 for its core bundling logic but configures it for incremental operation. It utilizes a FileMap or memory-resident FileSystem to avoid expensive disk I/O during the hot-reload cycle [src/runtime/server/server_body.rs:35-36].
Bake implements a "Barrel Optimization" strategy to defer loading unused submodules from pure re-export files. This uses a RequestedExports map to track which specific exports are needed [src/bundler/barrel_imports.zig:12-15]. During development, the DevServer persists barrel_needed_exports across builds to ensure that if a new file imports an existing export from a barrel, the barrel doesn't need to be fully re-parsed [src/bundler/barrel_imports.zig:111-120].
| Entity | Role in Code | Location |
|---|---|---|
DevServer | Main orchestrator of the development lifecycle | [src/runtime/bake/DevServer.rs:81] |
Transpiler | Handles TS/JSX to JS conversion for server/client/ssr | [src/runtime/bake/DevServer.rs:129-157] |
RequestedExports | Tracks all or partial export requests for barrel files | [src/bundler/barrel_imports.zig:12-15] |
RouteBundle | Container for a route's bundled code and assets | [src/runtime/bake/DevServer.rs:160] |
Sources: [src/runtime/bake/DevServer.rs:1-166], [src/bundler/barrel_imports.zig:1-120], [src/runtime/server/server_body.rs:10-12]
Bun's HMR system allows developers to update code without losing application state. The runtime on the client manages a registry of modules and their acceptance status.
When the server sends a hot update, the client-side fixture (e.g., client-fixture.mjs) or the browser runtime performs the following:
blob: URLs in testing environments) and replaces the previous module entry [test/bake/client-fixture.mjs:149-161].import.meta.hot.accept(), the update "bubbles" up the dependency graph. If no parent accepts the update, a full page reload is triggered [test/bake/dev/bundle.test.ts:199-202].DevServer retains the old styles to prevent the UI from breaking entirely [test/bake/dev/css.test.ts:6-12].HMR Entity Mapping
Sources: [src/runtime/bake/DevServer.rs:81-163], [test/bake/dev/bundle.test.ts:199-202], [test/bake/client-fixture.mjs:87-116]
When a build or runtime error occurs, Bake provides a specialized client-side overlay. Build failures, such as syntax errors or resolution failures, are serialized on the server using SerializedFailure and sent to the client to be displayed in the HMR overlay [src/runtime/bake/DevServer.rs:161].
The DevServer maintains a SourceMapStore to associate generated code with original source locations [src/runtime/bake/DevServer.rs:162]. This is critical for:
Assets (images, fonts, etc.) are served via a specialized Assets store [src/runtime/bake/DevServer.rs:52]. When a CSS file references an asset via url(), the dev server automatically tracks this dependency. If the asset on disk changes, the DevServer can trigger a re-fetch or HMR update for the dependent stylesheet [test/bake/dev/css.test.ts:167-188].
Sources: [src/runtime/bake/DevServer.rs:52-163], [test/bake/dev/css.test.ts:167-188], [test/bake/dev/sourcemap.test.ts:1-4]
Refresh this wiki
This wiki was recently refreshed. Please wait 1 day to refresh again.