rnue
JavaScript Packages

@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 codegen

Reference

rnue-codegen codegen [output-dir] [sources..] [options]

The positional output-dir overrides codegenConfig.outputDir.unreal. Positional source files and directories override codegenConfig.jsSrcsDir.

OptionAliasDefaultDescription
--platform <name>-pNo filterSelect specification files for one platform.
--exclude <regex>-eNo filterExclude source paths that match the regular expression.
--package-name <name>NoneNonePass an optional package name to React Native Codegen.
--assume-nonnullNonefalseEnable 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:

package.json
{
  "codegenConfig": {
    "name": "ExampleNative",
    "type": "modules",
    "jsSrcsDir": "specs",
    "outputDir": {
      "unreal": "unreal"
    }
  }
}

Reference

FieldRequiredDefaultDescription
nameYesNoneThe generated library, Unreal plugin, and Unreal module name.
typeNoNoneThe React Native codegen kind. Use modules for a TurboModule package.
jsSrcsDirNosrcA source file or directory, relative to the package root.
outputDir.unrealUnless the command supplies output-dirNoneThe 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/codegen

Add a package script that uses the rnue-codegen executable:

package.json
{
  "scripts": {
    "codegen": "rnue-codegen codegen"
  }
}

Generate the plugin

Run codegen from the native package or one of its subdirectories:

npm run codegen

The 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.hUnreal-native types.
Public/<Library>/Generated/<Library>Advanced.hPer-runtime module interface and factory registration.
Private/<Library>/Generated/<Library>Binding.cppJSI conversion, React binding, and runtime lifecycle handling.
Private/<Library>/<Library>JSI.hUpstream 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 rootPurpose
<Library>.upluginUnreal plugin descriptor.
Source/<Library>/<Library>.Build.csUnreal module rules.
Source/<Library>/Public/<Library>.hUnreal module declaration and service ownership.
Source/<Library>/Private/<Library>.cppService construction, registration, and shutdown.
Source/<Library>/Private/<Library>/<Library>Service.hUser-owned service factory declaration.
Source/<Library>/Private/<Library>/<Library>Service.cppUser-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.

On this page