Skip to content

Commit 17226c4

Browse files
Riccardo Cipolleschifacebook-github-bot
authored andcommitted
Align Codegen between iOS and Android (#33864)
Summary: Pull Request resolved: #33864 This Diff aligns the way in which iOS and Android codegen the modules and components. Android takes all the JS in the project root folder and creates components starting from there. iOS used to required to specify a specific path for each component, within a JSON field called `libraries`. This Diff let iOS work in the same way as android does **Backward compatibility:** This diff still support the old way for iOS, but we are deprecating it. ## Changelog [iOS][Added] - Support codegen from a single folder Reviewed By: cortinico Differential Revision: D36473005 fbshipit-source-id: f752f1e80cf95fa567514b1cd7c24c42a052b0a6
1 parent c19bc0b commit 17226c4

File tree

7 files changed

+309
-71
lines changed

7 files changed

+309
-71
lines changed

packages/rn-tester/BUCK

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ rn_library(
6565
"pfh:ReactNative_CommonInfrastructurePlaceholder",
6666
"supermodule:xplat/default/public.react_native.playground",
6767
],
68-
native_component_spec_name = "MyNativeViewSpec",
68+
native_component_spec_name = "AppSpecs",
6969
skip_processors = True,
7070
visibility = ["PUBLIC"],
7171
deps = [
@@ -317,7 +317,7 @@ rn_xplat_cxx_library2(
317317
reexport_all_header_dependencies = False,
318318
visibility = ["PUBLIC"],
319319
deps = [
320-
":generated_components-MyNativeViewSpec",
320+
":generated_components-AppSpecs",
321321
"//xplat/js/react-native-github:RCTFabricComponentViewsBase",
322322
],
323323
)

packages/rn-tester/NativeComponentExample/ios/RNTMyNativeViewComponentView.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
#import "RNTMyNativeViewComponentView.h"
99

10-
#import <react/renderer/components/MyNativeViewSpec/ComponentDescriptors.h>
11-
#import <react/renderer/components/MyNativeViewSpec/EventEmitters.h>
12-
#import <react/renderer/components/MyNativeViewSpec/Props.h>
13-
#import <react/renderer/components/MyNativeViewSpec/RCTComponentViewHelpers.h>
10+
#import <react/renderer/components/AppSpecs/ComponentDescriptors.h>
11+
#import <react/renderer/components/AppSpecs/EventEmitters.h>
12+
#import <react/renderer/components/AppSpecs/Props.h>
13+
#import <react/renderer/components/AppSpecs/RCTComponentViewHelpers.h>
1414

1515
#import "RCTFabricComponentsPlugins.h"
1616

packages/rn-tester/package.json

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,8 @@
3131
"ws": "^6.1.4"
3232
},
3333
"codegenConfig": {
34-
"libraries": [
35-
{
36-
"name": "ScreenshotManagerSpec",
37-
"type": "modules",
38-
"ios": {},
39-
"android": {},
40-
"jsSrcsDir": "NativeModuleExample"
41-
},
42-
{
43-
"name": "MyNativeViewSpec",
44-
"type": "components",
45-
"ios": {},
46-
"android": {},
47-
"jsSrcsDir": "NativeComponentExample/js"
48-
}
49-
]
34+
"name": "AppSpecs",
35+
"type": "all",
36+
"jsSrcsDir": "."
5037
}
5138
}

scripts/codegen/__test_fixtures__/fixtures.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,48 @@
1010

1111
'use-strict';
1212

13+
const SINGLE_LIBRARY_CODEGEN_CONFIG = {
14+
codegenConfig: {
15+
libraries: [
16+
{
17+
name: 'react-native',
18+
type: 'all',
19+
jsSrcsDir: '.',
20+
},
21+
],
22+
},
23+
};
24+
25+
const MULTIPLE_LIBRARIES_CODEGEN_CONFIG = {
26+
codegenConfig: {
27+
libraries: [
28+
{
29+
name: 'react-native',
30+
type: 'all',
31+
jsSrcsDir: '.',
32+
},
33+
{
34+
name: 'my-component',
35+
type: 'components',
36+
jsSrcsDir: 'component/js',
37+
},
38+
{
39+
name: 'my-module',
40+
type: 'module',
41+
jsSrcsDir: 'module/js',
42+
},
43+
],
44+
},
45+
};
46+
47+
const NO_LIBRARIES_CONFIG_FILE = {
48+
codegenConfig: {
49+
name: 'AppModules',
50+
type: 'all',
51+
jsSrcsDir: '.',
52+
},
53+
};
54+
1355
const SCHEMA_TEXT = `
1456
{
1557
"modules": {
@@ -84,4 +126,7 @@ const SCHEMA = JSON.parse(SCHEMA_TEXT);
84126
module.exports = {
85127
schemaText: SCHEMA_TEXT,
86128
schema: SCHEMA,
129+
noLibrariesConfigFile: NO_LIBRARIES_CONFIG_FILE,
130+
singleLibraryCodegenConfig: SINGLE_LIBRARY_CODEGEN_CONFIG,
131+
multipleLibrariesCodegenConfig: MULTIPLE_LIBRARIES_CODEGEN_CONFIG,
87132
};

scripts/codegen/__tests__/generate-artifacts-executor-test.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
'use strict';
1212

1313
const underTest = require('../generate-artifacts-executor');
14+
const fixtures = require('../__test_fixtures__/fixtures');
1415
const path = require('path');
1516

17+
const codegenConfigKey = 'codegenConfig';
18+
const reactNativeDependencyName = 'react-native';
19+
const rootPath = path.join(__dirname, '../../..');
20+
1621
describe('generateCode', () => {
1722
it('executeNodes with the right arguents', () => {
1823
// Define variables and expected values
@@ -69,3 +74,130 @@ describe('generateCode', () => {
6974
expect(mkdirSyncInvocationCount).toBe(2);
7075
});
7176
});
77+
78+
describe('extractLibrariesFromJSON', () => {
79+
it('throws if in react-native and no dependencies found', () => {
80+
let libraries = [];
81+
let configFile = {};
82+
expect(() => {
83+
underTest._extractLibrariesFromJSON(
84+
configFile,
85+
libraries,
86+
codegenConfigKey,
87+
);
88+
}).toThrow();
89+
});
90+
91+
it('it skips if not into react-native and no dependencies found', () => {
92+
let libraries = [];
93+
let configFile = {};
94+
95+
underTest._extractLibrariesFromJSON(
96+
configFile,
97+
libraries,
98+
codegenConfigKey,
99+
'some-node-module',
100+
'node_modules/some',
101+
);
102+
expect(libraries.length).toBe(0);
103+
});
104+
105+
it('extracts a single dependency when config has no libraries', () => {
106+
let libraries = [];
107+
let configFile = fixtures.noLibrariesConfigFile;
108+
underTest._extractLibrariesFromJSON(
109+
configFile,
110+
libraries,
111+
codegenConfigKey,
112+
'my-app',
113+
'.',
114+
);
115+
expect(libraries.length).toBe(1);
116+
expect(libraries[0]).toEqual({
117+
library: 'my-app',
118+
config: {
119+
name: 'AppModules',
120+
type: 'all',
121+
jsSrcsDir: '.',
122+
},
123+
libraryPath: '.',
124+
});
125+
});
126+
127+
it("extract codegenConfig when it's empty", () => {
128+
const configFile = {codegenConfig: {libraries: []}};
129+
let libraries = [];
130+
underTest._extractLibrariesFromJSON(
131+
configFile,
132+
codegenConfigKey,
133+
libraries,
134+
reactNativeDependencyName,
135+
rootPath,
136+
);
137+
expect(libraries.length).toBe(0);
138+
});
139+
140+
it('extract codegenConfig when dependency is one', () => {
141+
const configFile = fixtures.singleLibraryCodegenConfig;
142+
let libraries = [];
143+
underTest._extractLibrariesFromJSON(
144+
configFile,
145+
libraries,
146+
codegenConfigKey,
147+
reactNativeDependencyName,
148+
rootPath,
149+
);
150+
expect(libraries.length).toBe(1);
151+
expect(libraries[0]).toEqual({
152+
library: reactNativeDependencyName,
153+
config: {
154+
name: 'react-native',
155+
type: 'all',
156+
jsSrcsDir: '.',
157+
},
158+
libraryPath: rootPath,
159+
});
160+
});
161+
162+
it('extract codegenConfig with multiple dependencies', () => {
163+
const configFile = fixtures.multipleLibrariesCodegenConfig;
164+
const myDependency = 'my-dependency';
165+
const myDependencyPath = path.join(__dirname, myDependency);
166+
let libraries = [];
167+
underTest._extractLibrariesFromJSON(
168+
configFile,
169+
libraries,
170+
codegenConfigKey,
171+
myDependency,
172+
myDependencyPath,
173+
);
174+
expect(libraries.length).toBe(3);
175+
expect(libraries[0]).toEqual({
176+
library: myDependency,
177+
config: {
178+
name: 'react-native',
179+
type: 'all',
180+
jsSrcsDir: '.',
181+
},
182+
libraryPath: myDependencyPath,
183+
});
184+
expect(libraries[1]).toEqual({
185+
library: myDependency,
186+
config: {
187+
name: 'my-component',
188+
type: 'components',
189+
jsSrcsDir: 'component/js',
190+
},
191+
libraryPath: myDependencyPath,
192+
});
193+
expect(libraries[2]).toEqual({
194+
library: myDependency,
195+
config: {
196+
name: 'my-module',
197+
type: 'module',
198+
jsSrcsDir: 'module/js',
199+
},
200+
libraryPath: myDependencyPath,
201+
});
202+
});
203+
});

0 commit comments

Comments
 (0)