BakeNative is under active development. APIs may change.
Skip to Content
BakeNative

Ship as a Swift package

Build a BakeNative app into prebuilt XCFrameworks that an existing iOS or macOS app adds as a Swift package.

bake build --xcframework turns a BakeNative app into a Swift package of prebuilt binaries. A brownfield app adds the package, imports one module, and shows the screen with one line of Swift. It needs no other frameworks, no build settings, and no Swift/C++ interop.

1. Build the package

bake build --target ios --bake --xcframework            # or: --target ios,mac
dist/ios-spm/
  Package.swift                 product ExampleApp = the two binaries below (local paths)
  ExampleApp.xcframework        the app: app.hbc, app.baked, app.css, manifest.json as resources
                                + a generated Swift entry point
  BakeNative.xcframework           the runtime (BakeNativeKit on iOS, BakeNativeAppKit on macOS), Hermes inside
  *.xcframework.zip             the same binaries zipped for hosting
  Package.remote.swift          Package.swift with url: + checksum: for the zips

The product name comes from package.json (@bake-native/example-app becomes ExampleApp) or from --name. In the monorepo, the runtime is built on first use and cached until a native source changes (pnpm xcframework, below). Elsewhere, pass a prebuilt one with --runtime <BakeNative.xcframework>.

2. Add the package to the app

In Xcode, use File → Add Package Dependencies… → Add Local… and pick dist/ios-spm, or declare it in another package:

.package(path: "../my-feature/dist/ios-spm")

To host the binaries, upload the two zips and use Package.remote.swift as the package manifest (--remote-url sets the base URL). bake prints each target with its checksum:

.binaryTarget(name: "ExampleApp", url: "https://cdn.example.com/ExampleApp/1.0.0/ExampleApp.xcframework.zip",
              checksum: "9fba9363…"),
.binaryTarget(name: "BakeNative", url: "https://cdn.example.com/ExampleApp/1.0.0/BakeNative.xcframework.zip",
              checksum: "1da21deb…"),

3. One line of Swift

import ExampleApp

ExampleAppView()

The generated module re-exports the runtime (@_exported import BakeNativeKit / BakeNativeAppKit), so ExampleApp.bundle (an BakeNativeBundle) works with the lower-level API too, for example BakeNativeSurface(bundle: ExampleApp.bundle, onEvent: …).

On iPhone, add CADisableMinimumFrameDurationOnPhone (<true/>) to the host app's Info.plist so the screen runs at 120 Hz on ProMotion displays (120 Hz on ProMotion iPhones). The prebuilt runtime is compiled without DEBUG, so it does not warn when the key is missing.

How the runtime is built

pnpm xcframework (tools/build-xcframework/build.mjs) writes dist/xcframework/BakeNative.xcframework, with one framework per slice:

SliceFrameworkSize
ios-arm64BakeNativeKit.framework6.0 MB
ios-arm64-simulatorBakeNativeKit.framework6.1 MB
macos-arm64BakeNativeAppKit.framework (versioned bundle)6.7 MB
  • One Swift module per framework. BakeNativeApple (the Rust core driver, CoreText backend, the spec module runtime, SwiftUI controls, the SVG replay of the Rust bake-svg) and the platform host are compiled together, with the Rust core (libbake_abi.a) linked statically. The build copies the sources to a staging folder, drops the cross-module imports there, and leaves the repository sources unchanged. The frameworks are built with library evolution and ship only a .swiftinterface, so the interface is all a client sees.
  • C, C++ and Rust stay private. The Rust core (its bake_* C ABI and the Hermes bridge) is linked with hidden visibility. Swift imports the C headers through a private module map (internal import), so none appears in the interface. Every C++ symbol is unexported.
  • Hermes is linked statically. pnpm hermes also builds native/hermes/artifacts/static/<platform>/libhermes.a (Hermes' hermesvm_a + jsi + boost_context, the same build trees as hermesvm.framework). It is linked with -hidden-l, so the frameworks link only system libraries and export only Swift symbols. The build checks this every time (otool -L, nm -gU, and a grep of the interfaces).

Two binaries or one (--embed-runtime)

By default a package contains two binaries: the app framework and the shared runtime. Several features can share one copy of the runtime this way, and the runtime zip has the same checksum in every package built from the same runtime.

--embed-runtime compiles the runtime into <Product>.xcframework, so the package has one binary. The trade-off: every feature built this way carries its own copy of the runtime and Hermes (about 6 MB each). The runtime types belong to each feature's module, so FeatureA.BakeNativeBundle and FeatureB.BakeNativeBundle are different types. The copies don't clash, because all C++ symbols are hidden and Swift names are module-qualified. Use --embed-runtime when an app has a single feature.

Limits

  • arm64 only. The iOS simulator slice runs on Apple silicon Macs, so build the host app for the active architecture (Xcode's default for Debug).
  • The interfaces are written by the Swift 6.4 compiler (they use Module::Type selectors), so the app needs the same Xcode or a newer one.
  • App targets in Xcode (iOS and macOS) embed the frameworks and need no settings. A SwiftPM command-line executable on macOS runs as is with swift build --build-system native. SwiftPM's default build system does not add @executable_path to an executable's rpaths, so with it the executable needs linkerSettings: [.unsafeFlags(["-Xlinker", "-rpath", "-Xlinker", "@executable_path"])].

On this page