@rnue/codegen
Generate an Unreal TurboModule plugin from a TypeScript specification.
@rnue/codegen converts a React Native TypeScript spec into an Unreal C++ plugin. It also generates the boilerplate bridge, JSI bindings and a no-op service implementation based on the bridge contract.
After using @rnue/codegen to generate the native plugin you can use @rnue/cli to place it in an Unreal project.
Simple usage
You can run @rnue/codegen directly via npx at the root of a native package:
npx @rnue/codegen codegenReference
rnue-codegen codegen [output-dir] [sources..] [options]The positional output-dir overrides codegenConfig.outputDir.unreal. Positional source files and directories override codegenConfig.jsSrcsDir.
| Option | Alias | Default | Description |
|---|---|---|---|
--platform <name> | -p | No filter | Select specification files for one platform. |
--exclude <regex> | -e | No filter | Exclude source paths that match the regular expression. |
--package-name <name> | None | None | Pass an optional package name to React Native Codegen. |
--assume-nonnull | None | false | Enable the React Native Codegen non-null assumption. |
Codegen aborts when the selected sources contain no native modules.
Configuration
@rnue/codegen will look for a codegenConfig in the package.json file:
{
"codegenConfig": {
"name": "ExampleNative",
"type": "modules",
"jsSrcsDir": "specs",
"outputDir": {
"unreal": "unreal"
}
}
}Reference
| Field | Required | Default | Description |
|---|---|---|---|
name | Yes | None | The generated library, Unreal plugin, and Unreal module name. |
type | No | None | The React Native codegen kind. Use modules for a TurboModule package. |
jsSrcsDir | No | src | A source file or directory, relative to the package root. |
outputDir.unreal | Unless the command supplies output-dir | None | The parent directory for the generated Unreal plugin, relative to the package root. |
The example writes the plugin to unreal/ExampleNative.
Codegen always adds codegenConfig.name below the selected output directory.
Read Codegen Typings for the complete type map and runtime rules.
Advanced usage
Though you may invoke the command manually every time you need it, it is recommended to set it up as a development dependency in the rnue project.
Set up codegen as a script in package.json
Install the package as a development dependency of the native package:
npm i -D @rnue/codegenAdd a package script that uses the rnue-codegen executable:
{
"scripts": {
"codegen": "rnue-codegen codegen"
}
}Generate the plugin
Run codegen from the native package or one of its subdirectories:
npm run codegenThe command searches parent directories for the nearest package.json file with codegenConfig.
File ownership
Codegen replaces the following files on every successful generation:
Path below Source/<Library> | Purpose |
|---|---|
Public/<Library>/Generated/<Library>Spec.h | Unreal-native types. |
Public/<Library>/Generated/<Library>Advanced.h | Per-runtime module interface and factory registration. |
Private/<Library>/Generated/<Library>Binding.cpp | JSI conversion, React binding, and runtime lifecycle handling. |
Private/<Library>/<Library>JSI.h | Upstream React Native C++ specification. |
Do not edit these generated files.
Codegen creates the remaining scaffold files only when each file is absent:
| Path below the plugin root | Purpose |
|---|---|
<Library>.uplugin | Unreal plugin descriptor. |
Source/<Library>/<Library>.Build.cs | Unreal module rules. |
Source/<Library>/Public/<Library>.h | Unreal module declaration and service ownership. |
Source/<Library>/Private/<Library>.cpp | Service construction, registration, and shutdown. |
Source/<Library>/Private/<Library>/<Library>Service.h | User-owned service factory declaration. |
Source/<Library>/Private/<Library>/<Library>Service.cpp | User-owned service implementation. |
These files belong to the native package implementer after their creation. Later codegen runs do not replace their contents.
First time functionality
The initial service implementation contains one fail-fast stub for each schema method:
- Void and synchronous methods throw
FReactModuleError. - Typed Promise methods reject their
TReactPromise. - Raw JSI Promise methods throw synchronously.
After a schema change, regenerate the plugin. Then update the user-owned service implementation to match the generated interface.
The scaffold returns false from SupportsDynamicReloading(). Live React bindings can outlast a reloaded C++ module image.
Low-level commands
combine
Use combine to create a React Native schema JSON file:
rnue-codegen combine <output> [sources..] [options]combine accepts --platform (-p), --exclude (-e), and --library-name (-l).
generate
Use generate to create upstream pure C++ output from an existing schema:
rnue-codegen generate <schema> <library-name> <output-dir> [options]generate accepts --package-name and --assume-nonnull. It does not create the Unreal plugin scaffold or the Unreal-native service interface.