Skip to content

Commit dfa20a4

Browse files
troZeeokwasniewski
authored andcommitted
feat: add fabric template (#273)
1 parent 9f5bc45 commit dfa20a4

File tree

62 files changed

+793
-197
lines changed

Some content is hidden

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

62 files changed

+793
-197
lines changed

β€Žpackages/create-react-native-library/src/index.ts

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,47 @@ const COMMON_FILES = path.resolve(__dirname, '../templates/common');
1919
const JS_FILES = path.resolve(__dirname, '../templates/js-library');
2020
const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
2121
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
22-
const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example');
23-
const EXAMPLE_TURBO_FILES = path.resolve(
24-
__dirname,
25-
'../templates/example-turbo'
26-
);
22+
const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example-legacy');
2723
const NATIVE_COMMON_FILES = path.resolve(
2824
__dirname,
2925
'../templates/native-common'
3026
);
3127

3228
const NATIVE_FILES = {
3329
module_legacy: path.resolve(__dirname, '../templates/native-library-legacy'),
34-
module_turbo: path.resolve(__dirname, '../templates/native-library-turbo'),
30+
module_new: path.resolve(__dirname, '../templates/native-library-new'),
3531
module_mixed: path.resolve(__dirname, '../templates/native-library-mixed'),
36-
view: path.resolve(__dirname, '../templates/native-view-library'),
32+
view_legacy: path.resolve(__dirname, '../templates/native-view-legacy'),
33+
view_mixed: path.resolve(__dirname, '../templates/native-view-mixed'),
34+
view_new: path.resolve(__dirname, '../templates/native-view-new'),
3735
};
3836

3937
const JAVA_FILES = {
4038
module_legacy: path.resolve(__dirname, '../templates/java-library-legacy'),
41-
module_turbo: path.resolve(__dirname, '../templates/java-library-turbo'),
39+
module_new: path.resolve(__dirname, '../templates/java-library-new'),
4240
module_mixed: path.resolve(__dirname, '../templates/java-library-mixed'),
43-
view: path.resolve(__dirname, '../templates/java-view-library'),
41+
view_legacy: path.resolve(__dirname, '../templates/java-view-legacy'),
42+
view_mixed: path.resolve(__dirname, '../templates/java-view-mixed'),
43+
view_new: path.resolve(__dirname, '../templates/java-view-new'),
4444
};
4545

4646
const OBJC_FILES = {
4747
module: path.resolve(__dirname, '../templates/objc-library'),
48-
view: path.resolve(__dirname, '../templates/objc-view-library'),
48+
view_legacy: path.resolve(__dirname, '../templates/objc-view-legacy'),
49+
view_mixed: path.resolve(__dirname, '../templates/objc-view-mixed'),
50+
view_new: path.resolve(__dirname, '../templates/objc-view-new'),
4951
};
5052

5153
const KOTLIN_FILES = {
5254
module_legacy: path.resolve(__dirname, '../templates/kotlin-library-legacy'),
53-
module_turbo: path.resolve(__dirname, '../templates/kotlin-library-turbo'),
55+
module_new: path.resolve(__dirname, '../templates/kotlin-library-new'),
5456
module_mixed: path.resolve(__dirname, '../templates/kotlin-library-mixed'),
55-
view: path.resolve(__dirname, '../templates/kotlin-view-library'),
57+
view_legacy: path.resolve(__dirname, '../templates/kotlin-view-legacy'),
5658
};
5759

5860
const SWIFT_FILES = {
59-
module: path.resolve(__dirname, '../templates/swift-library'),
60-
view: path.resolve(__dirname, '../templates/swift-view-library'),
61+
module: path.resolve(__dirname, '../templates/swift-library-legacy'),
62+
view: path.resolve(__dirname, '../templates/swift-view-legacy'),
6163
};
6264

6365
type ArgName =
@@ -84,7 +86,14 @@ type Answers = {
8486
| 'kotlin-objc'
8587
| 'kotlin-swift'
8688
| 'cpp';
87-
type?: 'module-legacy' | 'module-turbo' | 'module-mixed' | 'view' | 'library';
89+
type?:
90+
| 'module-legacy'
91+
| 'module-new'
92+
| 'module-mixed'
93+
| 'view'
94+
| 'view-mixed'
95+
| 'view-new'
96+
| 'library';
8897
example?: 'expo' | 'native';
8998
};
9099

@@ -131,6 +140,8 @@ const args: Record<ArgName, yargs.Options> = {
131140
'module-turbo',
132141
'module-mixed',
133142
'view',
143+
'view-mixed',
144+
'view-new',
134145
'library',
135146
],
136147
},
@@ -278,18 +289,31 @@ async function create(argv: yargs.Arguments<any>) {
278289
},
279290
{
280291
title: 'Turbo module (experimental)',
281-
value: 'module-turbo',
292+
value: 'module-new',
282293
},
283294
{
284295
title: 'Native module',
285296
value: 'module-legacy',
286297
},
287298
{ title: 'Native view', value: 'view' },
299+
{
300+
title: 'Native fabric view with backward compat (experimental)',
301+
value: 'view-mixed',
302+
},
303+
{
304+
title: 'Native fabric view (experimental)',
305+
value: 'view-new',
306+
},
288307
{ title: 'JavaScript library', value: 'library' },
289308
],
290309
},
291310
'languages': {
292-
type: (_, values) => (values.type !== 'library' ? 'select' : null),
311+
type: (_, values) =>
312+
values.type === 'library' ||
313+
values.type === 'view-new' ||
314+
values.type === 'view-mixed'
315+
? null
316+
: 'select',
293317
name: 'languages',
294318
message: 'Which languages do you want to use?',
295319
choices: (_, values) => {
@@ -298,7 +322,7 @@ async function create(argv: yargs.Arguments<any>) {
298322
{ title: 'Kotlin & Objective-C', value: 'kotlin-objc' },
299323
];
300324

301-
if (values.type !== 'module-turbo' && values.type !== 'module-mixed') {
325+
if (values.type !== 'module-new' && values.type !== 'module-mixed') {
302326
languages.push(
303327
{ title: 'Java & Swift', value: 'java-swift' },
304328
{ title: 'Kotlin & Swift', value: 'kotlin-swift' }
@@ -376,17 +400,14 @@ async function create(argv: yargs.Arguments<any>) {
376400
version = FALLBACK_BOB_VERSION;
377401
}
378402

379-
const moduleType = type === 'view' ? 'view' : 'module';
380-
403+
const moduleType = type.startsWith('view') ? 'view' : 'module';
381404
const architecture =
382-
type === 'module-turbo'
383-
? 'turbo'
384-
: type === 'module-mixed'
405+
type === 'module-new' || type === 'view-new'
406+
? 'new'
407+
: type === 'module-mixed' || type === 'view-mixed'
385408
? 'mixed'
386409
: 'legacy';
387410

388-
const turbomodule = architecture === 'turbo' || architecture === 'mixed';
389-
390411
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
391412

392413
let namespace: string | undefined;
@@ -424,11 +445,11 @@ async function create(argv: yargs.Arguments<any>) {
424445
identifier: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
425446
native: languages !== 'js',
426447
architecture,
427-
turbomodule,
428448
cpp: languages === 'cpp',
429449
kotlin: languages === 'kotlin-objc' || languages === 'kotlin-swift',
430450
swift: languages === 'java-swift' || languages === 'kotlin-swift',
431-
view: type === 'view',
451+
view: moduleType === 'view',
452+
module: moduleType === 'module',
432453
},
433454
author: {
434455
name: authorName,
@@ -500,33 +521,45 @@ async function create(argv: yargs.Arguments<any>) {
500521
path.join(folder, 'example')
501522
);
502523

503-
if (turbomodule) {
504-
await copyDir(
505-
path.join(EXAMPLE_TURBO_FILES, 'example'),
506-
path.join(folder, 'example')
507-
);
508-
}
509-
510524
await copyDir(NATIVE_COMMON_FILES, folder);
511525

512526
if (moduleType === 'module') {
513527
await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder);
514528
} else {
515-
await copyDir(NATIVE_FILES[moduleType], folder);
529+
await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder);
516530
}
517531

518532
if (options.project.swift) {
519533
await copyDir(SWIFT_FILES[moduleType], folder);
520534
} else {
521-
await copyDir(OBJC_FILES[moduleType], folder);
535+
if (moduleType === 'module') {
536+
await copyDir(OBJC_FILES[moduleType], folder);
537+
} else {
538+
await copyDir(OBJC_FILES[`view_${architecture}`], folder);
539+
}
522540
}
523541

524-
const android_files = options.project.kotlin ? KOTLIN_FILES : JAVA_FILES;
525-
526-
if (moduleType === 'module') {
527-
await copyDir(android_files[`${moduleType}_${architecture}`], folder);
542+
if (options.project.kotlin) {
543+
switch (`${moduleType}_${architecture}`) {
544+
case 'module_legacy':
545+
await copyDir(KOTLIN_FILES['module_legacy'], folder);
546+
break;
547+
case 'module_mixed':
548+
await copyDir(KOTLIN_FILES['module_mixed'], folder);
549+
break;
550+
case 'module_new':
551+
await copyDir(KOTLIN_FILES['module_new'], folder);
552+
break;
553+
case 'view_legacy':
554+
await copyDir(KOTLIN_FILES['view_legacy'], folder);
555+
break;
556+
default:
557+
console.log(
558+
`Kotlin template for ${moduleType}_${architecture} has not been implemented`
559+
);
560+
}
528561
} else {
529-
await copyDir(android_files[moduleType], folder);
562+
await copyDir(JAVA_FILES[`${moduleType}_${architecture}`], folder);
530563
}
531564

532565
if (options.project.cpp) {

β€Žpackages/create-react-native-library/src/utils/generateExampleApp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default async function generateExampleApp({
4040
type: 'expo' | 'native';
4141
dest: string;
4242
projectName: string;
43-
architecture: 'turbo' | 'mixed' | 'legacy';
43+
architecture: 'new' | 'mixed' | 'legacy';
4444
}) {
4545
const directory = path.join(dest, 'example');
4646
const args =
@@ -97,7 +97,7 @@ export default async function generateExampleApp({
9797

9898
// Temporary until autolinking is fixed on iOS
9999
// https://github.com/facebook/react-native/commit/a5622165c198ac6e7ffff5883d4f269a2c974f2e
100-
if (architecture === 'turbo' || architecture === 'mixed') {
100+
if (architecture === 'new' || architecture === 'mixed') {
101101
scripts.postinstall = 'patch-package';
102102

103103
Object.assign(devDependencies, PACKAGES_TO_ADD_NEW_ARCH);
@@ -109,7 +109,7 @@ export default async function generateExampleApp({
109109
);
110110

111111
// If the library is on new architecture, enable new arch for iOS and Android
112-
if (architecture === 'turbo') {
112+
if (architecture === 'new') {
113113
// Android
114114
// Change newArchEnabled=false to newArchEnabled=true in example/android/gradle.properties
115115
const gradleProperties = await fs.readFile(

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"@release-it/conventional-changelog": "^5.0.0",
6262
"@types/jest": "^28.1.2",
6363
"@types/react": "~17.0.21",
64-
"@types/react-native": "0.68.0",
64+
"@types/react-native": "0.70.0",
6565
"commitlint": "^17.0.2",
6666
"eslint": "^8.4.1",
6767
"eslint-config-prettier": "^8.5.0",
@@ -70,7 +70,7 @@
7070
"pod-install": "^0.1.0",
7171
"prettier": "^2.0.5",
7272
"react": "17.0.2",
73-
"react-native": "0.68.2",
73+
"react-native": "0.70.0",
7474
"react-native-builder-bob": "^<%- bob.version %>",
7575
"release-it": "^15.0.0",
7676
"typescript": "^4.5.2"
@@ -154,11 +154,11 @@
154154
}
155155
]
156156
]
157-
<% if (project.turbomodule) { -%>
157+
<% if (project.architecture === "new") { -%>
158158
},
159159
"codegenConfig": {
160-
"name": "RN<%- project.name -%>Spec",
161-
"type": "modules",
160+
"name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec",
161+
"type": <%- project.view ? '"components"': '"modules"' %>,
162162
"jsSrcsDir": "src"
163163
<% } -%>
164164
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ By default, the example is configured to build with the old architecture. To run
5151
```
5252

5353
<% } -%>
54-
<% if (project.architecture === 'turbo' || project.architecture == 'mixed') { -%>
54+
<% if (project.architecture === 'new' || project.architecture == 'mixed') { -%>
5555
To confirm that the app is running with the new architecture, you can check the Metro logs for a message like this:
5656

5757
```sh

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,35 @@ npm install <%- project.slug %>
1111
## Usage
1212

1313
<% if (project.view) { -%>
14+
1415
```js
1516
import { <%- project.name -%>View } from "<%- project.slug -%>";
1617

1718
// ...
1819

1920
<<%- project.name -%>View color="tomato" />
2021
```
21-
<% } else if (project.architecture == 'turbo') { -%>
22+
23+
<% } else if (project.architecture === 'new' && project.module) { -%>
24+
2225
```js
23-
import { multiply } from "<%- project.slug -%>";
26+
import { multiply } from '<%- project.slug -%>';
2427

2528
// ...
2629

2730
const result = multiply(3, 7);
2831
```
32+
2933
<% } else { -%>
34+
3035
```js
31-
import { multiply } from "<%- project.slug -%>";
36+
import { multiply } from '<%- project.slug -%>';
3237

3338
// ...
3439

3540
const result = await multiply(3, 7);
3641
```
42+
3743
<% } -%>
3844

3945
## Contributing

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function App() {
1616
</View>
1717
);
1818
}
19-
<% } else if (project.architecture == 'turbo') { -%>
19+
<% } else if (project.architecture === 'new' && project.module) { -%>
2020
const result = multiply(3, 7);
2121

2222
export default function App() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require('path');
2+
const pak = require('../package.json');
3+
4+
module.exports = {
5+
dependencies: {
6+
[pak.name]: {
7+
root: path.join(__dirname, '..'),
8+
},
9+
},
10+
};

0 commit comments

Comments
Β (0)