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

Embed in a macOS app

Show a BakeNative bundle in a SwiftUI or AppKit window.

Add the BakeNativeAppKit product (Installation) and copy dist/mac from bake build --target mac --bake into your app as a folder.

SwiftUI

import BakeNativeAppKit
import SwiftUI

struct ContentView: View {
  let url = Bundle.main.url(forResource: "mac", withExtension: nil)!

  var body: some View {
    BakeNativeSurface(bundle: BakeNativeBundle(directory: url), hydration: .load) { host in
      host.root.onEvent = { event in
        if case .error(let message) = event { print("BakeNative:", message) }
      }
    }
  }
}

BakeNativeSurface launches the bundle as soon as it has a size. The closure runs once with the underlying BakeNativeView, which is where you attach event handlers.

AppKit

import BakeNativeAppKit

let host = BakeNativeView(hydration: .load)
host.root.onEvent = { event in /* metrics, batches, console output, errors */ }
window.contentView = host
host.launch(BakeNativeBundle(directory: url))

BakeNativeView is a scroll view. It scrolls when your page is taller than the window, uses your page's background color behind the content, and lays the page out again when the window resizes.

Window size

The snapshot holds no geometry: the view lays your first screen out at its real size when it launches, so any window size works. Responsive CSS (media queries, viewport units) is resolved for that size too. If the window is resized or the appearance changes while the snapshot is still showing, it is laid out again before your app takes over.

Shipping the app

To hand the screen to another app as prebuilt binaries (Hermes linked in, nothing else to embed), see Ship as a Swift package.

If you build the app in Xcode, adding the package is enough: Xcode embeds the Hermes framework in the app for you.

If you build a SwiftPM executable and package the .app yourself, copy hermesvm.framework into Contents/Frameworks and add @executable_path/../Frameworks to the executable's rpath. The repository's examples/mac/package-app.sh does exactly this and is a good template.

Props and native modules

BakeNativeSurface(bundle: bundle, props: ["user": "ada"], modules: BakeNativeBundleModules.root())

let host = BakeNativeView(modules: BakeNativeBundleModules.root())   // builtinModules: true by default
host.launch(bundle, props: ["user": "ada"])

See Native modules.

Several screens

Every BakeNativeView runs its own Hermes runtime and keeps no global state, so you can show several bundles at once, in separate windows or side by side.

On this page