Setup
In-app Turbo Module
For an in-app module, follow the instructions in Build an App to create your starting app.Standalone Turbo Module
For a standalone module, you can start from a Turbo Module template. From the root of your package, run the command below to see a list of available templates.idl-turbo-module: If you are consuming an OS API (IDL)basic-turbo-moduleIf you are creating a Turbo Module from your own code
IDL Turbo Module
--idlPackage: The IDL package to consume in the Turbo Module. This is formatted in reverse DNS notation, for examplecom.amazondeveloper.kepler.foo.--outputDir: [Optional] The directory in which to generate the project. If not specified, the project is generated in the folder in which you ran the command.
Plain Turbo Module
--outputDir: [Optional] The directory in which to generate the project. If not specified, the project is generated in the folder in which you ran the command.
Build a Turbo Module
Step 1: Install TurbomoduleAPI
If you created your Turbo Module using a template, the@amazon-devices/keplerscript-turbomodule-api package is already listed under the dependencies section of your package.json and you can skip this step.
From the root of your package, run the following command.
Step 2: Create interface definitions in TypeScript
If you used a Turbo Module template, this file already exists, but you must to modify it to suit your needs. The file NativePinger.ts (note: The file is named based on the name you passed to the template) defines the native module’s interface to JavaScript. By convention, it is located insrc/turbo-modules.
The interface should look similar to this:
Pinger which exposes a single function ping that returns a number. For more information on what return types are available, see JavaScript types in native code.
TurboModuleRegistry.getEnforcing uses 'Pinger' as a lookup key to load the module’s native object, and raises an exception if it is not found. Then, we bind the Pinger interface to the returned object.
Step 3: Run codegen to generate native scaffolding
The TurbomoduleAPI library includes a codegen tool that allows you to generate C++ scaffolding based on your TypeScript interface. The generated code defines the C++ specification for your Turbo Module. To use codegen, run the following commands.npx keplerscript-turbomodule-api codegen -h or from the Vega Codegen FAQ.
Step 4: Implement C++ functions
Typically, you won’t need to manually modify the files created by codegen under thekepler/turbo-modules/generated/ directory. If you do, be aware that rerunning codegen will overwrite these files. To avoid losing updates, cache your modified files elsewhere and reapply your modifications after rerunning codegen.
The C++ (.h/.cpp) files in the kepler/turbo-modules/ directory are where you will implement the interface. You will need to fill out function bodies generated by the previous step.
IDL
If your Turbo Module uses an OS API via an IDL, you need to add imports, instantiate the IDL components, and use the API as needed. Example Add the imports.com.amazondeveloper.kepler.foo should match the --idlPackage you passed to the template, if any.
For a more complete example, see Internationalization Developer Guide.
Step 5: Implement JavaScript layer
The TypeScript interface you create provides type information that allows the native object to be called from JavaScript. You might want an additional layer of JavaScript logic over the native methods for operations such as error handling, JavaScript logging, or argument preprocessing for optional parameters or any unsupported types. Even if you don’t need such logic right now, you can allow for more flexibility to address future API changes if you add a basic wrapper to separate the JavaScript and native APIs.export {default as Pinger} from './turbo-modules/Pinger' in its index.ts.
Adding JavaScript tests
You can text your JavaScript code with a framework such as Jest. The C++ Turbo Module instance will not exist when running tests with Jest, so you need to mock it for your JavaScript tests. The following example show how you can mcok your C++ Turbo Module instance.__mocks__ subdirectory adjacent to the module, such as src/turbo-modules/__mocks__/NativePinger.ts.
Mocks can be provided to your Turbo Module’s consumers to facilitate their own testing. For example, you can provide a mocks file which they can pass to Jest’s setupFiles.
Step 6: Add build configurations
If you have generated your Turbo Module from a template, this step should already be completed. However, you might still find the information useful if you made adjustments to the code provided by the template or specified additional codegen parameters.Autolinking
Vega Turbo Modules are autolinked, meaning the native module is not directly linked by applications, but instead loaded by React Native for Vega according to the app’s dependencies and package configuration. As a Turbo Module developer, you will need to add some Turbo Module registration and configuration code.Step 1: Register the module in AutoLinkInit.cpp
Inkepler/AutoLinkInit.cpp, you should have the following code, adjusted for your Turbo Module.
TurboModuleRegistry.getEnforcing, and you can use KEPLER_REGISTER_TURBO_MODULE(namespace, classname) as above. If you passed a classname which is different from the string used by TurboModuleRegistry, you can use the KEPLER_REGISTER_TURBO_MODULE_WITH_NAME(TurboModuleRegistry namestring, namespace, classname) macro.
step 2: Add metadata in react-native.config.js
All Turbo Module types specify their Autolinking configuration in react-native.config.js. For standalone Turbo Module Libraries.Dynamic linking
linkDynamic controls whether the autolink library is loaded at app launch (increasing launch latency), or loaded when the Turbo Module is requested by JavaScript. Regardless of the setting in your library, an application can override the value in their own react-native.config.js to what works better for their needs. If you’re not sure, set the default to true.
CMakeLists.txt
TheCMakeLists.txt defines the build process for your project and contains commands and instructions that CMake uses to generate “makefiles”, or project files, for various compilers and IDEs. You should have a CMakeLists.txt in your project root similar to the following.
Build targeting
For in-app Turbo Modules, build targeting should be done following the app setup instructions. Build targeting can be specified viapackage.json, or via CLI build flags.
Step 7: Build the Turbo Module
package.json scripts to run commands commonly used in your team, or piecemeal run existing scripts.
Step 8: Use your new Turbo Module in an app
For an in-app Turbo Module, you can import the module with a relative path and call it as you would any standard JS module.Rebuilding
If you install a standalone Turbo Module to an app using the .tgz file generated by npm pack, and later make changes to the Turbo Module, you need to reinstall the new .tgz after rebuilding the Turbo Module. This process is generally:- Make Turbo Module code changes
- Rebuild the Turbo Module (npm pack)
- Run
- Rebuild the app

