Skip to content

Commit 946310a

Browse files
committed
Add optional React Compiler support as build feature
Add React Compiler as an optional optimization feature that can be enabled via USE_REACT_COMPILER CMake option. When enabled, Babel preprocesses React components to add automatic memoization before bundling with esbuild. Changes: - Add babel-plugin-react-compiler and glob to package.json devDependencies - Create .babelrc.cjs with conditional React Compiler plugin - Implement two-pass build in bundle-react-unit.js (Babel → esbuild) - Add USE_REACT_COMPILER CMake option (OFF by default) - Pass environment variable from CMake to build script - Add .babelrc.cjs to build dependencies for proper rebuild tracking The feature is zero-cost when disabled - no Babel overhead, identical esbuild configuration, and no behavior changes. When enabled, components are automatically optimized with memoization to reduce re-renders.
1 parent 298c2cf commit 946310a

File tree

8 files changed

+639
-28
lines changed

8 files changed

+639
-28
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.babelrc.cjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-react', { runtime: 'automatic' }]
4+
],
5+
plugins: [
6+
process.env.USE_REACT_COMPILER === 'true'
7+
? 'babel-plugin-react-compiler'
8+
: null
9+
].filter(Boolean)
10+
};

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ if(NOT DEFINED REACT_BUNDLE_MODE)
3131
endif()
3232
set(REACT_BUNDLE_MODE ${REACT_BUNDLE_MODE} CACHE STRING "React bundle compilation mode (0=native, 1=bytecode, 2=source)")
3333

34+
# React Compiler: Optional optimization feature (OFF by default)
35+
option(USE_REACT_COMPILER "Enable React Compiler for automatic memoization optimizations" OFF)
36+
3437
message(STATUS "Hermes build: ${HERMES_BUILD}")
3538
message(STATUS "Hermes source: ${HERMES_SRC}")
3639
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
3740
message(STATUS "React bundle mode: ${REACT_BUNDLE_MODE} (0=native, 1=bytecode, 2=source)")
41+
message(STATUS "React Compiler: ${USE_REACT_COMPILER}")
3842

3943
# Collect reconciler library files for dependency tracking
4044
# This is defined at root level so it can be reused by multiple apps

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ _**Note**: This project is an independent experiment and is not affiliated with,
6262
- Lightning-fast startup - no parsing, no JIT warmup, just native code execution
6363
- Single executable distribution
6464
-**React 19.2.0** with custom reconciler
65+
-**React Compiler support** - Optional automatic memoization optimization
6566
-**Static Hermes** with zero-overhead FFI to Dear ImGui
6667
-**Event loop** with `setTimeout`, `setImmediate`, and Promise support
6768
-**React hooks** (`useState`, `useEffect`, etc.)
@@ -994,6 +995,21 @@ Override the mode explicitly:
994995
cmake -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug -DREACT_BUNDLE_MODE=1
995996
```
996997

998+
### React Compiler (Optional)
999+
1000+
React Compiler is an optional build feature that automatically adds memoization to React components. It's disabled by default and can be enabled via CMake:
1001+
1002+
```bash
1003+
cmake -B build -DUSE_REACT_COMPILER=ON
1004+
cmake --build build
1005+
```
1006+
1007+
**How it works:**
1008+
- Uses a two-pass build: Babel preprocessing → esbuild bundling
1009+
- Babel transforms React components to add `useMemoCache` calls
1010+
- Components skip re-renders when props/state haven't changed
1011+
- Works with all compilation modes (native/bytecode/source)
1012+
9971013
### Hermes Build Integration
9981014

9991015
Hermes is **automatically** cloned and built as part of the CMake configuration—no manual setup required:

cmake/react-imgui.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,23 @@ function(add_react_imgui_app)
8080
${RECONCILER_FILES}
8181
${APP_FILES}
8282
${CMAKE_SOURCE_DIR}/scripts/bundle-react-unit.js
83+
${CMAKE_SOURCE_DIR}/.babelrc.cjs
8384
)
8485
if(ARG_ADDITIONAL_JS_DEPS)
8586
list(APPEND REACT_UNIT_DEPS ${ARG_ADDITIONAL_JS_DEPS})
8687
endif()
8788

8889
# Bundle with esbuild
8990
add_custom_command(OUTPUT ${REACT_UNIT_BUNDLE}
90-
COMMAND node ${CMAKE_SOURCE_DIR}/scripts/bundle-react-unit.js
91+
COMMAND ${CMAKE_COMMAND} -E env
92+
USE_REACT_COMPILER=$<IF:$<BOOL:${USE_REACT_COMPILER}>,true,false>
93+
node ${CMAKE_SOURCE_DIR}/scripts/bundle-react-unit.js
9194
${ARG_ENTRY_POINT}
9295
${REACT_UNIT_BUNDLE}
9396
$<IF:$<CONFIG:Debug>,development,production>
9497
DEPENDS ${REACT_UNIT_DEPS}
9598
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
96-
COMMENT "Bundling ${ARG_TARGET} React unit with esbuild (NODE_ENV=$<IF:$<CONFIG:Debug>,development,production>)"
99+
COMMENT "Bundling ${ARG_TARGET} React unit with esbuild (NODE_ENV=$<IF:$<CONFIG:Debug>,development,production>, React Compiler=${USE_REACT_COMPILER})"
97100
)
98101

99102
# Compile based on REACT_BUNDLE_MODE

0 commit comments

Comments
 (0)