Nâng CaoReact Native iconReact Native

TurboModule spec file (`.ts`) → Codegen flow chi tiết?

Codegen là middleware giữa spec TypeScript và native binding code. Hiểu flow giúp debug khi build fail.

Input:
- Một hoặc nhiều file Native*.ts (theo convention) trong thư mục được khai báo codegenConfig.jsSrcsDir.
- Mỗi file phải export interface Spec extends TurboModule { ... }export default TurboModuleRegistry.getEnforcing<Spec>('ModuleName').

Bước Codegen chạy:

1. Parse: đọc tất cả Native*.ts, dùng TypeScript compiler API extract type info.
2. Validate: kiểm tra type hợp lệ — chỉ accept string, number, boolean, Object, Array, Promise<T>, custom Object literal. Type khác (Date, Map) → error.
3. Generate:
- iOS: RN<Name>Spec.h (Obj-C protocol), RN<Name>Spec.mm (Obj-C++ wrapper với JSI bindings).
- Android: <Name>Spec.java (abstract class với @ReactMethod annotations) hoặc <Name>Spec.kt.
- C++: <Name>JSI.h (TurboModule descriptor cho JSI runtime).
4. Output location:
- iOS: ios/build/generated/ios/.
- Android: android/build/generated/source/codegen/.
- Tự động add vào Xcode project và Gradle source set.

Khi nào trigger:
- iOS: pod install mỗi khi spec file đổi.
- Android: Gradle task generateCodegenArtifactsFromSchema chạy mỗi build.
- Manual: node node_modules/react-native/scripts/generate-codegen-artifacts.js để debug.

Debug flow:

bash
# In ra schema được parse
node node_modules/react-native/scripts/generate-codegen-artifacts.js \
  --path . --outputPath ./codegen-debug

Kết quả ./codegen-debug/schema.json cho thấy Codegen hiểu spec ra sao — match với generated code.

Common errors:
- "Cannot read properties of undefined (reading 'name')": spec không export đúng default.
- "Unsupported type: Date": dùng string (ISO date) hoặc number (unix timestamp) thay.
- Build cache stale: cd ios && rm -rf build && pod install.

Xem toàn bộ React Native cùng filter theo level & chủ đề con.

Mở danh sách React Native