Skip to content

Commit cc95a72

Browse files
committed
feat: add a template for turbo module (experimental)
1 parent 3d7af12 commit cc95a72

File tree

74 files changed

+841
-899
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+841
-899
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@ const JS_FILES = path.resolve(__dirname, '../templates/js-library');
1818
const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
1919
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
2020
const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example');
21+
const EXAMPLE_TURBO_FILES = path.resolve(
22+
__dirname,
23+
'../templates/example-turbo'
24+
);
2125
const NATIVE_COMMON_FILES = path.resolve(
2226
__dirname,
2327
'../templates/native-common'
2428
);
2529

2630
const NATIVE_FILES = {
27-
module: path.resolve(__dirname, '../templates/native-library'),
31+
module_legacy: path.resolve(__dirname, '../templates/native-library-legacy'),
32+
module_turbo: path.resolve(__dirname, '../templates/native-library-turbo'),
33+
module_mixed: path.resolve(__dirname, '../templates/native-library-mixed'),
2834
view: path.resolve(__dirname, '../templates/native-view-library'),
2935
};
3036

3137
const JAVA_FILES = {
32-
module: path.resolve(__dirname, '../templates/java-library'),
38+
module_legacy: path.resolve(__dirname, '../templates/java-library-legacy'),
39+
module_turbo: path.resolve(__dirname, '../templates/java-library-turbo'),
40+
module_mixed: path.resolve(__dirname, '../templates/java-library-mixed'),
3341
view: path.resolve(__dirname, '../templates/java-view-library'),
3442
};
3543

@@ -71,9 +79,8 @@ type Answers = {
7179
| 'java-swift'
7280
| 'kotlin-objc'
7381
| 'kotlin-swift'
74-
| 'cpp'
75-
| 'js';
76-
type?: 'module' | 'view';
82+
| 'cpp';
83+
type?: 'module-legacy' | 'module-turbo' | 'module-mixed' | 'view' | 'library';
7784
example?: 'expo' | 'native';
7885
};
7986

@@ -228,8 +235,38 @@ async function create(argv: yargs.Arguments<any>) {
228235
},
229236
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
230237
},
231-
'languages': {
238+
'type': {
232239
type: 'select',
240+
name: 'type',
241+
message: 'What type of library do you want to develop?',
242+
choices: [
243+
...(process.env.EXPERIMENTAL_TURBO_MODULES === '1'
244+
? [
245+
{
246+
title: 'Turbo module (backward compatible)',
247+
value: 'module-mixed',
248+
},
249+
{
250+
title: 'Turbo module',
251+
value: 'module-turbo',
252+
},
253+
]
254+
: []),
255+
{
256+
title: 'Native module',
257+
value: 'module-legacy',
258+
},
259+
{ title: 'Native view', value: 'view' },
260+
{ title: 'JavaScript library', value: 'library' },
261+
],
262+
},
263+
'languages': {
264+
type: (_, values) =>
265+
values.type === 'js' ||
266+
values.type === 'module-turbo' ||
267+
values.type === 'module-mixed'
268+
? null
269+
: 'select',
233270
name: 'languages',
234271
message: 'Which languages do you want to use?',
235272
choices: [
@@ -238,25 +275,10 @@ async function create(argv: yargs.Arguments<any>) {
238275
{ title: 'Kotlin & Objective-C', value: 'kotlin-objc' },
239276
{ title: 'Kotlin & Swift', value: 'kotlin-swift' },
240277
{ title: 'C++ for both iOS & Android', value: 'cpp' },
241-
{ title: 'JavaScript only', value: 'js' },
242-
],
243-
},
244-
'type': {
245-
type: (prev: string) =>
246-
['java-objc', 'java-swift', 'kotlin-objc', 'kotlin-swift'].includes(
247-
prev
248-
)
249-
? 'select'
250-
: null,
251-
name: 'type',
252-
message: 'What type of library do you want to develop?',
253-
choices: [
254-
{ title: 'Native module (to expose native APIs)', value: 'module' },
255-
{ title: 'Native view (to use as a component)', value: 'view' },
256278
],
257279
},
258280
'example': {
259-
type: (prev: string) => (prev === 'js' ? 'select' : null),
281+
type: (_, values) => (values.type === 'js' ? 'select' : null),
260282
name: 'example',
261283
message: 'What type of example app do you want to generate?',
262284
choices: [
@@ -276,8 +298,8 @@ async function create(argv: yargs.Arguments<any>) {
276298
authorEmail,
277299
authorUrl,
278300
repoUrl,
279-
languages,
280-
type = 'module',
301+
type = 'module-mixed',
302+
languages = type === 'library' ? 'js' : 'java-objc',
281303
example = 'native',
282304
} = {
283305
...argv,
@@ -321,6 +343,17 @@ async function create(argv: yargs.Arguments<any>) {
321343
version = FALLBACK_BOB_VERSION;
322344
}
323345

346+
const moduleType = type === 'view' ? 'view' : 'module';
347+
348+
const architecture =
349+
type === 'module-turbo'
350+
? 'turbo'
351+
: type === 'module-mixed'
352+
? 'mixed'
353+
: 'legacy';
354+
355+
const turbomodule = architecture === 'turbo' || architecture === 'mixed';
356+
324357
const options = {
325358
bob: {
326359
version: version || FALLBACK_BOB_VERSION,
@@ -334,11 +367,12 @@ async function create(argv: yargs.Arguments<any>) {
334367
package: slug.replace(/[^a-z0-9]/g, '').toLowerCase(),
335368
identifier: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
336369
native: languages !== 'js',
370+
architecture,
371+
turbomodule,
337372
cpp: languages === 'cpp',
338373
kotlin: languages === 'kotlin-objc' || languages === 'kotlin-swift',
339374
swift: languages === 'java-swift' || languages === 'kotlin-swift',
340-
module: languages !== 'js',
341-
moduleType: type,
375+
view: type === 'view',
342376
},
343377
author: {
344378
name: authorName,
@@ -396,19 +430,35 @@ async function create(argv: yargs.Arguments<any>) {
396430
path.join(folder, 'example')
397431
);
398432

433+
if (turbomodule) {
434+
await copyDir(
435+
path.join(EXAMPLE_TURBO_FILES, 'example'),
436+
path.join(folder, 'example')
437+
);
438+
}
439+
399440
await copyDir(NATIVE_COMMON_FILES, folder);
400-
await copyDir(NATIVE_FILES[type], folder);
441+
442+
if (moduleType === 'module') {
443+
await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder);
444+
} else {
445+
await copyDir(NATIVE_FILES[moduleType], folder);
446+
}
401447

402448
if (options.project.swift) {
403-
await copyDir(SWIFT_FILES[type], folder);
449+
await copyDir(SWIFT_FILES[moduleType], folder);
404450
} else {
405-
await copyDir(OBJC_FILES[type], folder);
451+
await copyDir(OBJC_FILES[moduleType], folder);
406452
}
407453

408454
if (options.project.kotlin) {
409-
await copyDir(KOTLIN_FILES[type], folder);
455+
await copyDir(KOTLIN_FILES[moduleType], folder);
410456
} else {
411-
await copyDir(JAVA_FILES[type], folder);
457+
if (moduleType === 'module') {
458+
await copyDir(JAVA_FILES[`${moduleType}_${architecture}`], folder);
459+
} else {
460+
await copyDir(JAVA_FILES[moduleType], folder);
461+
}
412462
}
413463

414464
if (options.project.cpp) {

packages/create-react-native-library/templates/common/$package.json

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "<%- project.slug %>",
2+
"name": "<%- project.slug -%>",
33
"version": "0.1.0",
44
"description": "test",
55
"main": "lib/commonjs/index",
@@ -13,7 +13,7 @@
1313
"android",
1414
"ios",
1515
"cpp",
16-
"<%- project.identifier %>.podspec",
16+
"<%- project.identifier -%>.podspec",
1717
"!lib/typescript/example",
1818
"!android/build",
1919
"!ios/build",
@@ -28,21 +28,21 @@
2828
"prepare": "bob build",
2929
"release": "release-it",
3030
"example": "yarn --cwd example",
31-
"pods": "cd example && pod-install --quiet",
31+
"pods": "cd example && RCT_NEW_ARCH_ENABLED=<%- project.turbomodule ? 1 : 0 -%> pod-install --quiet",
3232
"bootstrap": "yarn example && yarn && yarn pods"
3333
},
3434
"keywords": [
3535
"react-native",
3636
"ios",
3737
"android"
3838
],
39-
"repository": "<%- repo %>",
40-
"author": "<%- author.name %> <<%- author.email %>> (<%- author.url %>)",
39+
"repository": "<%- repo -%>",
40+
"author": "<%- author.name -%> <<%- author.email -%>> (<%- author.url -%>)",
4141
"license": "MIT",
4242
"bugs": {
43-
"url": "<%- repo %>/issues"
43+
"url": "<%- repo -%>/issues"
4444
},
45-
"homepage": "<%- repo %>#readme",
45+
"homepage": "<%- repo -%>#readme",
4646
"publishConfig": {
4747
"registry": "https://registry.npmjs.org/"
4848
},
@@ -54,7 +54,7 @@
5454
"@release-it/conventional-changelog": "^5.0.0",
5555
"@types/jest": "^28.1.2",
5656
"@types/react": "~17.0.21",
57-
"@types/react-native": "0.67.9",
57+
"@types/react-native": "0.68.0",
5858
"commitlint": "^17.0.2",
5959
"eslint": "^8.4.1",
6060
"eslint-config-prettier": "^8.5.0",
@@ -148,5 +148,16 @@
148148
}
149149
]
150150
]
151+
<% if (project.turbomodule) { -%>
152+
},
153+
"codegenConfig": {
154+
"libraries": [
155+
{
156+
"name": "RN<%- project.name -%>Spec",
157+
"type": "modules",
158+
"jsSrcsDir": "src"
159+
}
160+
]
161+
<% } -%>
151162
}
152163
}

packages/create-react-native-library/templates/common/CONTRIBUTING.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ To run the example app on iOS:
3131
```sh
3232
yarn example ios
3333
```
34-
<% if (!project.native) { %>
34+
<% if (!project.native) { -%>
3535
To run the example app on Web:
3636

3737
```sh
3838
yarn example web
3939
```
40-
<% } %>
40+
<% } -%>
4141
Make sure your code passes TypeScript and ESLint. Run the following to verify:
4242

4343
```sh
@@ -56,11 +56,11 @@ Remember to add tests for your change if possible. Run the unit tests by:
5656
```sh
5757
yarn test
5858
```
59-
<% if (project.native) { %>
60-
To edit the Objective-C files, open `example/ios/<%- project.name %>Example.xcworkspace` in XCode and find the source files at `Pods > Development Pods > <%- project.slug %>`.
59+
<% if (project.native) { -%>
60+
To edit the Objective-C files, open `example/ios/<%- project.name -%>Example.xcworkspace` in XCode and find the source files at `Pods > Development Pods > <%- project.slug -%>`.
6161

62-
To edit the Kotlin files, open `example/android` in Android studio and find the source files at `<%- project.package %>` under `Android`.
63-
<% } %>
62+
To edit the Kotlin files, open `example/android` in Android studio and find the source files at `<%- project.package -%>` under `Android`.
63+
<% } -%>
6464
### Commit message convention
6565

6666
We follow the [conventional commits specification](https://www.conventionalcommits.org/en) for our commit messages:

packages/create-react-native-library/templates/common/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 <%- author.name %>
3+
Copyright (c) 2022 <%- author.name -%>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

packages/create-react-native-library/templates/common/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# <%- project.slug %>
1+
# <%- project.slug -%>
22

3-
<%- project.description %>
3+
<%- project.description -%>
44

55
## Installation
66

77
```sh
8-
npm install <%- project.slug %>
8+
npm install <%- project.slug -%>
99
```
1010

1111
## Usage
1212

13-
<% if (project.moduleType === "view") { -%>
13+
<% if (project.view) { -%>
1414
```js
15-
import { <%- project.name %>View } from "<%- project.slug %>";
15+
import { <%- project.name -%>View } from "<%- project.slug -%>";
1616

1717
// ...
1818

19-
<<%- project.name %>View color="tomato" />
19+
<<%- project.name -%>View color="tomato" />
2020
```
2121
<% } else { -%>
2222
```js
23-
import { multiply } from "<%- project.slug %>";
23+
import { multiply } from "<%- project.slug -%>";
2424

2525
// ...
2626

packages/create-react-native-library/templates/common/example/src/App.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import * as React from 'react';
22

3-
<% if (project.moduleType === "view") { -%>
3+
<% if (project.view) { -%>
44
import { StyleSheet, View } from 'react-native';
5-
import { <%- project.name %>View } from '<%- project.slug %>';
5+
import { <%- project.name -%>View } from '<%- project.slug -%>';
66
<% } else { -%>
77
import { StyleSheet, View, Text } from 'react-native';
8-
import { multiply } from '<%- project.slug %>';
8+
import { multiply } from '<%- project.slug -%>';
99
<% } -%>
1010

11-
<% if (project.moduleType === "view") { -%>
11+
<% if (project.view) { -%>
1212
export default function App() {
1313
return (
1414
<View style={styles.container}>
15-
<<%- project.name %>View color="#32a852" style={styles.box} />
15+
<<%- project.name -%>View color="#32a852" style={styles.box} />
1616
</View>
1717
);
1818
}

packages/create-react-native-library/templates/common/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"baseUrl": "./",
44
"paths": {
5-
"<%- project.slug %>": ["./src/index"]
5+
"<%- project.slug -%>": ["./src/index"]
66
},
77
"allowUnreachableCode": false,
88
"allowUnusedLabels": false,

packages/create-react-native-library/templates/cpp-library/android/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set (CMAKE_CXX_STANDARD 11)
55

66
add_library(cpp
77
SHARED
8-
../cpp/<%- project.identifier %>.cpp
8+
../cpp/<%- project.identifier -%>.cpp
99
cpp-adapter.cpp
1010
)
1111

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <jni.h>
2-
#include "<%- project.identifier %>.h"
2+
#include "<%- project.identifier -%>.h"
33

44
extern "C"
55
JNIEXPORT jint JNICALL
6-
Java_com_<%- project.package %>_<%- project.name %>Module_nativeMultiply(JNIEnv *env, jclass type, jint a, jint b) {
6+
Java_com_<%- project.package -%>_<%- project.name -%>Module_nativeMultiply(JNIEnv *env, jclass type, jint a, jint b) {
77
return example::multiply(a, b);
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include "<%- project.identifier %>.h"
1+
#include "<%- project.identifier -%>.h"
22

33
namespace example {
4-
int multiply(float a, float b) {
4+
int multiply(double a, double b) {
55
return a * b;
66
}
77
}

0 commit comments

Comments
 (0)