Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Adding or changing turbo-module templates

In addition to generating the bindings between Hermes and Rust, uniffi-bindgen-react-native generates the files needed to run this as a turbo-module. The list of files are documented elsewhere in this book.

Templates are written for Rinja templating library.

Changing the templates for these files is relatively simple. This PR is a good example of adding a file.

Adding a new template

  1. Add new template to the codegen/templates directory.
  2. Add a new RenderedFile struct, which specifies the template, and its path to right codegen.rs file (here are the codegen.rs files for JSI Android, JSI iOS, JSI crossplatform and WASM).
  3. Add an entry to the list of generated files in this book.

The template context will have quite a lot of useful information data-structures about the project; the most prominent:

Testing changes

The ./scripts/test-turbo-modules.sh script runs a suite of tests, simulating the steps in the Getting Started tutorial:

  • do the names of the ubrn generated files match up with the files builder-bob generated files?
  • do generated identifiers in the ubrn generated files match up with those generated by builder-bob?
  • does an Android app compile
  • does an iOS app compile

Help wanted

This does not yet run continuous integration.

If you want to test a particular configuration, you can use the command with a series of options:

Usage: ./scripts/test-turbo-modules.sh [options] [PROJECT_DIR]

Options:
  -A, --android                      Build for Android.
  -I, --ios                          Build for iOS.
  -C, --ubrn-config                  Use a ubrn config file.
  -T, --app-tsx                      Use a App.tsx file.

  -s, --slug PROJECT_SLUG            Specify the project slug (default: my-test-library).
  -i, --ios-name IOS_NAME            Specify the iOS project name (default: MyTestLibrary).

  -u, --builder-bob-version VERSION  Specify the version of builder-bob to use (default: latest).
  -k, --keep-directory-on-exit       Keep the PROJECT_DIR directory even if an error does not occur.
  -f, --force-new-directory          If PROJECT_DIR directory exist, remove it first.
  -h, --help                         Display this help message.

Arguments:
  PROJECT_DIR                        Specify the root directory for the project (default: my-test-library).

For example, to test if a configuration builds then runs:

fixtures=./integration/fixtures/turbo-module-testing
directory=/tmp/my-test-library
./scripts/test-turbo-modules.sh \
    --ubrn-config $fixtures/ubrn.config.yaml \
    --app-tsx $fixtures/App.tsx \
    --ios \
    --android \
    --keep-directory-on-exit \
    --force-new-directory \
    "$directory"

cd "$directory"
yarn example start