Skip to content

Commit 3cedb09

Browse files
authored
[Codegen] Exclude unlinked libs from codegen (#47712)
1 parent 33fce44 commit 3cedb09

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

packages/react-native/scripts/codegen/generate-artifacts-executor.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ function extractLibrariesFromJSON(configFile, dependencyPath) {
152152
const config = configFile.codegenConfig;
153153
return [
154154
{
155+
libraryName: configFile.name,
155156
config,
156157
libraryPath: dependencyPath,
157158
},
@@ -251,19 +252,23 @@ function findExternalLibraries(pkgJson, projectRoot) {
251252
});
252253
}
253254

254-
function findLibrariesFromReactNativeConfig(projectRoot) {
255+
function readRNConfigJSFile(projectRoot) {
255256
const rnConfigFileName = 'react-native.config.js';
256257

257-
console.log(
258-
`\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${rnConfigFileName}`,
259-
);
260-
261258
const rnConfigFilePath = path.resolve(projectRoot, rnConfigFileName);
262259

263260
if (!fs.existsSync(rnConfigFilePath)) {
264261
return [];
265262
}
266-
const rnConfig = require(rnConfigFilePath);
263+
return require(rnConfigFilePath);
264+
}
265+
266+
function findLibrariesFromReactNativeConfig(projectRoot) {
267+
console.log(
268+
`\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in react-native.config.js`,
269+
);
270+
271+
const rnConfig = readRNConfigJSFile(projectRoot);
267272

268273
if (rnConfig.dependencies == null) {
269274
return [];
@@ -289,6 +294,33 @@ function findLibrariesFromReactNativeConfig(projectRoot) {
289294
});
290295
}
291296

297+
// Function to look for libraries explicitly unlinked from the app
298+
// through the react-native.config.js file.
299+
// If this happens, it might be that the app does not need
300+
// to generate code for that library as it won't be used by that platform
301+
// @return { [libraryName: string]: [platform: string] }
302+
function findNotLinkedLibraries(projectRoot) {
303+
const rnConfig = readRNConfigJSFile(projectRoot);
304+
305+
if (rnConfig.dependencies == null) {
306+
return {};
307+
}
308+
309+
let notLinkedLibraries = {};
310+
311+
Object.keys(rnConfig.dependencies).forEach(name => {
312+
const dependency = rnConfig.dependencies[name];
313+
let notLinkedPlatforms = [];
314+
Object.keys(dependency.platforms).forEach(platform => {
315+
if (dependency.platforms[platform] == null) {
316+
notLinkedPlatforms.push(platform);
317+
}
318+
});
319+
notLinkedLibraries[name] = notLinkedPlatforms
320+
});
321+
return notLinkedLibraries;
322+
}
323+
292324
function findProjectRootLibraries(pkgJson, projectRoot) {
293325
console.log('[Codegen] Searching for codegen-enabled libraries in the app.');
294326

@@ -694,6 +726,8 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {
694726
let platforms =
695727
targetPlatform === 'all' ? supportedPlatforms : [targetPlatform];
696728

729+
const notLinkedLibraries = findNotLinkedLibraries(projectRoot);
730+
697731
for (const platform of platforms) {
698732
const outputPath = computeOutputPath(
699733
projectRoot,
@@ -702,7 +736,15 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {
702736
platform,
703737
);
704738

705-
const schemaInfos = generateSchemaInfos(libraries);
739+
const schemaInfos = generateSchemaInfos(libraries.filter(library => {
740+
const unlinkedPlatforms = notLinkedLibraries[library.libraryName];
741+
if (unlinkedPlatforms && unlinkedPlatforms.includes(platform)) {
742+
console.log(`[Codegen - ${library.libraryName}] Skipping Codegen on ${platform}`);
743+
return false;
744+
}
745+
return true;
746+
}));
747+
706748
generateNativeCode(
707749
outputPath,
708750
schemaInfos.filter(schemaInfo =>

0 commit comments

Comments
 (0)