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

Modules with bake add

Copy native modules from a registry into your app, keep them up to date with three-way merges, and write your own from a spec.

Native modules such as the clipboard, haptics or the share sheet are not npm packages. bake add copies a module's source into your app, and from then on the app owns it: you read it, edit it and commit it like your own code. The glue that connects it to each platform (registration, type conversion, thread hops) is generated on every build, so it always matches your BakeNative version.

pnpm exec bake add clipboard
native/clipboard/Clipboard.native.ts
native/clipboard/Clipboard.kt
native/clipboard/Clipboard.web.ts
native/clipboard/clipboard.rs
bake-modules.json

Only the implementations for the targets in bake.config.ts are copied. bake create starts an app with clipboard, linking, share and haptics already added.

Use a module

The spec (Clipboard.native.ts) types native.Clipboard:

import { native } from '@bake-native/react';

await native.Clipboard.writeText('hello');
const text = await native.Clipboard.readText();

The next bake build registers the module on every target the bundle calls it on. It goes into .bake/native/<target>, which the host projects already include, so you never edit Xcode, Gradle or Cargo files.

Fails closed

A bundle that calls native.Haptics on a target with no implementation in native/haptics is a build error (module-haptics). The build also refuses a locked module file that was deleted, and a file that still has conflict markers from bake upgrade.

Both renderer packages expose native at their root. Shared module specs use types from @bake-native/core so either renderer can use the same implementation.

The official registry at https://bake-native.com/r/bake publishes format 2 with the module’s supported bake version range. The CLI also accepts format 1 registries.

The lock

bake-modules.json records, for each module, the registry it came from, its version, the hash of the version and the hash of every file copied. Commit it. Changing a file is fine: the lock only says what the registry gave you, so that bake diff and bake upgrade can tell your edits apart from the registry's changes.

The app lock stays at format 1. When opening an existing app, the CLI also reads its legacy oct-modules.json. A diff or dry run leaves that file and its recorded hashes intact. The first write migrates it to bake-modules.json. If both locks are present, the CLI stops so you can resolve which history belongs to the app.

See changes: bake diff

pnpm exec bake diff            # every module
pnpm exec bake diff clipboard --stat

bake diff shows your edits against the version you took, and the registry's changes from that version to its latest, as unified diffs. It also lists files of targets you added since bake add. To copy those files, run bake add <name> again; your edits stay.

Update: bake upgrade

pnpm exec bake upgrade clipboard --dry-run
pnpm exec bake upgrade clipboard

bake upgrade merges the registry's new version into your files three ways, with the version in the lock as the base:

  • a part that only the registry changed takes the registry's change;

  • a part that only you changed keeps yours;

  • a part that you both changed differently is left with conflict markers, and the command exits 1:

    <<<<<<< local (native/clipboard)
        your line
    ||||||| clipboard@2.0.0
        the old line
    =======
        the registry's new line
    >>>>>>> clipboard@2.1.0

Resolve the markers, then build. Nothing is overwritten silently. bake remove clipboard deletes a module you did not edit (--force deletes it anyway).

Other registries

A registry is static files: index.json and one <name>/<version>.json per version. Any web host or directory can serve one. Name them in bake.config.ts:

export default defineConfig({
  targets: ['ios', 'android'],
  registries: {
    acme: 'https://registry.acme.dev/r',
    corp: { url: 'https://modules.corp.example/r', headers: { Authorization: 'Bearer ${CORP_TOKEN}' } },
  },
});
pnpm exec bake add acme/camera@1.2.0

${NAME} in a header comes from the environment, so tokens stay out of the config and the lock. A published version never changes: bake checks every version and file against its hash, and refuses one that changed in place.

Write a module

The same steps work for you and for an agent:

  1. Scaffold. pnpm exec bake native new BatteryLevel --spec writes native/battery-level/: the spec BatteryLevel.native.ts and one skeleton for each target in bake.config.ts (.swift, .kt, .rs, .web.ts).
  2. Spec first. Describe the module in BatteryLevel.native.ts: member names, JSON-like types, sync or async, and the error codes. Every implementation follows it (Native features: spec first).
  3. Implement each target to match the spec. Call it from src/ with native.BatteryLevel.
  4. Test each implementation (module.invoke(...) in the Swift and Kotlin tests).
  5. Check. pnpm exec bake check builds every target in the config and fails on any target without an implementation.

To share the module, put the folder in a registry's modules/<name>/ with a module.json (name, version, description), then run bake registry build.

On this page