Skip to content

feat: add fabric option #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8e8e114
feat: initial fabric cli
MateWW Aug 25, 2022
d5aec16
feat: fabric root directory
MateWW Aug 25, 2022
dbde595
feat: fabric src dir
MateWW Aug 25, 2022
677b08b
feat: fabric ios dir
MateWW Aug 25, 2022
9f1febf
feat: generate example app via `react-native init`
atlj Sep 6, 2022
a06282b
chore(example template): remove android files
atlj Sep 6, 2022
3d2482f
chore(example template): remove unused root files
atlj Sep 6, 2022
98f8e40
chore(example template): remove ios files
atlj Sep 6, 2022
795d980
chore(example template): remove android buildscripts
atlj Sep 6, 2022
93223d1
refactor: move rn example app generator to utils
atlj Sep 6, 2022
d7fa53f
feat: tbc
MateWW Sep 6, 2022
25bb21b
chore(example template): rename index.tsx to index.ts
atlj Sep 6, 2022
8bd1521
feat: enable new architecture on turbo module libraries
atlj Sep 6, 2022
1e3c167
feat: rename to new_arch
Sep 6, 2022
e1aacb6
refactor: rename app exampleAppGenerators to generateRNApp
atlj Sep 6, 2022
910769d
refactor: don't specify RN version for example apps
atlj Sep 6, 2022
8d3e444
refactor(example generator): extract files to delete to constant
atlj Sep 6, 2022
3e1548b
refactor(rn example generator): extract packages to add and remove
atlj Sep 6, 2022
2f01afb
fix: native example code is generated on expo projects
atlj Sep 6, 2022
a112c16
fix: example
Sep 6, 2022
a0390ea
fix: MainComponentsRegistry headers
Sep 6, 2022
f7fb47d
refactor(example generator): change isTurbomodule flag as isNewArch
atlj Sep 6, 2022
950881a
docs(example generator): remove version param from init command
atlj Sep 6, 2022
e5a5e8f
chore: revert naming
Sep 7, 2022
0a27b16
feat: implement fabric native view
Sep 7, 2022
6fd32f3
feat: set root package's react and react native versions from example…
atlj Sep 7, 2022
813862f
fix: lint
Sep 7, 2022
9e3a313
chore: add a patch for react-native 0.70.0 for codegen on ios
atlj Sep 7, 2022
72097b4
fix(example template): tests cant import `App.tsx`
atlj Sep 7, 2022
5a23737
fix(example generator): react-test-renderer and react-native types ar…
atlj Sep 8, 2022
bac2efa
chore: unify filenames
Sep 8, 2022
881aaf4
Merge branch '@atlj/integrate-react-native-init' into MateWW/fabric-t…
Sep 8, 2022
6d19ea5
fix: java view mixed path
Sep 13, 2022
bc926bc
fix: App.tsx template
Sep 13, 2022
dbfaf38
chore: bump react native version
Sep 13, 2022
bdb6dce
fix: App.tsx new arch
Sep 14, 2022
8ac7b5d
chore: bump @types/react-native to 0.70.0
Sep 14, 2022
fc40661
Merge branch 'main' into MateWW/fabric-template
Oct 24, 2022
a066c22
remove patch-package
Oct 24, 2022
1daacea
add fix flag
Oct 24, 2022
59d628c
- Remove commands
Oct 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 74 additions & 41 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,47 @@ const COMMON_FILES = path.resolve(__dirname, '../templates/common');
const JS_FILES = path.resolve(__dirname, '../templates/js-library');
const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example');
const EXAMPLE_TURBO_FILES = path.resolve(
__dirname,
'../templates/example-turbo'
);
const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example-legacy');
const NATIVE_COMMON_FILES = path.resolve(
__dirname,
'../templates/native-common'
);

const NATIVE_FILES = {
module_legacy: path.resolve(__dirname, '../templates/native-library-legacy'),
module_turbo: path.resolve(__dirname, '../templates/native-library-turbo'),
module_new: path.resolve(__dirname, '../templates/native-library-new'),
module_mixed: path.resolve(__dirname, '../templates/native-library-mixed'),
view: path.resolve(__dirname, '../templates/native-view-library'),
view_legacy: path.resolve(__dirname, '../templates/native-view-legacy'),
view_mixed: path.resolve(__dirname, '../templates/native-view-mixed'),
view_new: path.resolve(__dirname, '../templates/native-view-new'),
};

const JAVA_FILES = {
module_legacy: path.resolve(__dirname, '../templates/java-library-legacy'),
module_turbo: path.resolve(__dirname, '../templates/java-library-turbo'),
module_new: path.resolve(__dirname, '../templates/java-library-new'),
module_mixed: path.resolve(__dirname, '../templates/java-library-mixed'),
view: path.resolve(__dirname, '../templates/java-view-library'),
view_legacy: path.resolve(__dirname, '../templates/java-view-legacy'),
view_mixed: path.resolve(__dirname, '../templates/java-view-mixed'),
view_new: path.resolve(__dirname, '../templates/java-view-new'),
};

const OBJC_FILES = {
module: path.resolve(__dirname, '../templates/objc-library'),
view: path.resolve(__dirname, '../templates/objc-view-library'),
view_legacy: path.resolve(__dirname, '../templates/objc-view-legacy'),
view_mixed: path.resolve(__dirname, '../templates/objc-view-mixed'),
view_new: path.resolve(__dirname, '../templates/objc-view-new'),
};

const KOTLIN_FILES = {
module_legacy: path.resolve(__dirname, '../templates/kotlin-library-legacy'),
module_turbo: path.resolve(__dirname, '../templates/kotlin-library-turbo'),
module_new: path.resolve(__dirname, '../templates/kotlin-library-new'),
module_mixed: path.resolve(__dirname, '../templates/kotlin-library-mixed'),
view: path.resolve(__dirname, '../templates/kotlin-view-library'),
view_legacy: path.resolve(__dirname, '../templates/kotlin-view-legacy'),
};

const SWIFT_FILES = {
module: path.resolve(__dirname, '../templates/swift-library'),
view: path.resolve(__dirname, '../templates/swift-view-library'),
module: path.resolve(__dirname, '../templates/swift-library-legacy'),
view: path.resolve(__dirname, '../templates/swift-view-legacy'),
};

type ArgName =
Expand All @@ -84,7 +86,14 @@ type Answers = {
| 'kotlin-objc'
| 'kotlin-swift'
| 'cpp';
type?: 'module-legacy' | 'module-turbo' | 'module-mixed' | 'view' | 'library';
type?:
| 'module-legacy'
| 'module-new'
| 'module-mixed'
| 'view'
| 'view-mixed'
| 'view-new'
| 'library';
example?: 'expo' | 'native';
};

Expand Down Expand Up @@ -131,6 +140,8 @@ const args: Record<ArgName, yargs.Options> = {
'module-turbo',
'module-mixed',
'view',
'view-mixed',
'view-new',
'library',
],
},
Expand Down Expand Up @@ -278,18 +289,31 @@ async function create(argv: yargs.Arguments<any>) {
},
{
title: 'Turbo module (experimental)',
value: 'module-turbo',
value: 'module-new',
},
{
title: 'Native module',
value: 'module-legacy',
},
{ title: 'Native view', value: 'view' },
{
title: 'Native fabric view with backward compat (experimental)',
value: 'view-mixed',
},
{
title: 'Native fabric view (experimental)',
value: 'view-new',
},
{ title: 'JavaScript library', value: 'library' },
],
},
'languages': {
type: (_, values) => (values.type !== 'library' ? 'select' : null),
type: (_, values) =>
values.type === 'library' ||
values.type === 'view-new' ||
values.type === 'view-mixed'
? null
: 'select',
name: 'languages',
message: 'Which languages do you want to use?',
choices: (_, values) => {
Expand All @@ -298,7 +322,7 @@ async function create(argv: yargs.Arguments<any>) {
{ title: 'Kotlin & Objective-C', value: 'kotlin-objc' },
];

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

const moduleType = type === 'view' ? 'view' : 'module';

const moduleType = type.startsWith('view') ? 'view' : 'module';
const architecture =
type === 'module-turbo'
? 'turbo'
: type === 'module-mixed'
type === 'module-new' || type === 'view-new'
? 'new'
: type === 'module-mixed' || type === 'view-mixed'
? 'mixed'
: 'legacy';

const turbomodule = architecture === 'turbo' || architecture === 'mixed';

const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');

let namespace: string | undefined;
Expand Down Expand Up @@ -424,11 +445,11 @@ async function create(argv: yargs.Arguments<any>) {
identifier: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
native: languages !== 'js',
architecture,
turbomodule,
cpp: languages === 'cpp',
kotlin: languages === 'kotlin-objc' || languages === 'kotlin-swift',
swift: languages === 'java-swift' || languages === 'kotlin-swift',
view: type === 'view',
view: moduleType === 'view',
module: moduleType === 'module',
},
author: {
name: authorName,
Expand Down Expand Up @@ -500,33 +521,45 @@ async function create(argv: yargs.Arguments<any>) {
path.join(folder, 'example')
);

if (turbomodule) {
await copyDir(
path.join(EXAMPLE_TURBO_FILES, 'example'),
path.join(folder, 'example')
);
}

await copyDir(NATIVE_COMMON_FILES, folder);

if (moduleType === 'module') {
await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder);
} else {
await copyDir(NATIVE_FILES[moduleType], folder);
await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder);
}

if (options.project.swift) {
await copyDir(SWIFT_FILES[moduleType], folder);
} else {
await copyDir(OBJC_FILES[moduleType], folder);
if (moduleType === 'module') {
await copyDir(OBJC_FILES[moduleType], folder);
} else {
await copyDir(OBJC_FILES[`view_${architecture}`], folder);
}
}

const android_files = options.project.kotlin ? KOTLIN_FILES : JAVA_FILES;

if (moduleType === 'module') {
await copyDir(android_files[`${moduleType}_${architecture}`], folder);
if (options.project.kotlin) {
switch (`${moduleType}_${architecture}`) {
case 'module_legacy':
await copyDir(KOTLIN_FILES['module_legacy'], folder);
break;
case 'module_mixed':
await copyDir(KOTLIN_FILES['module_mixed'], folder);
break;
case 'module_new':
await copyDir(KOTLIN_FILES['module_new'], folder);
break;
case 'view_legacy':
await copyDir(KOTLIN_FILES['view_legacy'], folder);
break;
default:
console.log(
`Kotlin template for ${moduleType}_${architecture} has not been implemented`
);
}
} else {
await copyDir(android_files[moduleType], folder);
await copyDir(JAVA_FILES[`${moduleType}_${architecture}`], folder);
}

if (options.project.cpp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default async function generateExampleApp({
type: 'expo' | 'native';
dest: string;
projectName: string;
architecture: 'turbo' | 'mixed' | 'legacy';
architecture: 'new' | 'mixed' | 'legacy';
}) {
const directory = path.join(dest, 'example');
const args =
Expand Down Expand Up @@ -97,7 +97,7 @@ export default async function generateExampleApp({

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

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

// If the library is on new architecture, enable new arch for iOS and Android
if (architecture === 'turbo') {
if (architecture === 'new') {
// Android
// Change newArchEnabled=false to newArchEnabled=true in example/android/gradle.properties
const gradleProperties = await fs.readFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
"@types/react-native": "0.68.0",
"@types/react-native": "0.70.0",
"commitlint": "^17.0.2",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -70,7 +70,7 @@
"pod-install": "^0.1.0",
"prettier": "^2.0.5",
"react": "17.0.2",
"react-native": "0.68.2",
"react-native": "0.70.0",
"react-native-builder-bob": "^<%- bob.version %>",
"release-it": "^15.0.0",
"typescript": "^4.5.2"
Expand Down Expand Up @@ -154,11 +154,11 @@
}
]
]
<% if (project.turbomodule) { -%>
<% if (project.architecture === "new") { -%>
},
"codegenConfig": {
"name": "RN<%- project.name -%>Spec",
"type": "modules",
"name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec",
"type": <%- project.view ? '"components"': '"modules"' %>,
"jsSrcsDir": "src"
<% } -%>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ By default, the example is configured to build with the old architecture. To run
```

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

```sh
Expand Down
12 changes: 9 additions & 3 deletions packages/create-react-native-library/templates/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ npm install <%- project.slug %>
## Usage

<% if (project.view) { -%>

```js
import { <%- project.name -%>View } from "<%- project.slug -%>";

// ...

<<%- project.name -%>View color="tomato" />
```
<% } else if (project.architecture == 'turbo') { -%>

<% } else if (project.architecture === 'new' && project.module) { -%>

```js
import { multiply } from "<%- project.slug -%>";
import { multiply } from '<%- project.slug -%>';

// ...

const result = multiply(3, 7);
```

<% } else { -%>

```js
import { multiply } from "<%- project.slug -%>";
import { multiply } from '<%- project.slug -%>';

// ...

const result = await multiply(3, 7);
```

<% } -%>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid adding new lines here


## Contributing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function App() {
</View>
);
}
<% } else if (project.architecture == 'turbo') { -%>
<% } else if (project.architecture === 'new' && project.module) { -%>
const result = multiply(3, 7);

export default function App() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');
const pak = require('../package.json');

module.exports = {
dependencies: {
[pak.name]: {
root: path.join(__dirname, '..'),
},
},
};
Loading