Skip to content

Commit

Permalink
[Codegen] Exclude unlinked libs from codegen (#47712)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipolleschi authored Dec 4, 2024
1 parent 33fce44 commit 3cedb09
Showing 1 changed file with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function extractLibrariesFromJSON(configFile, dependencyPath) {
const config = configFile.codegenConfig;
return [
{
libraryName: configFile.name,
config,
libraryPath: dependencyPath,
},
Expand Down Expand Up @@ -251,19 +252,23 @@ function findExternalLibraries(pkgJson, projectRoot) {
});
}

function findLibrariesFromReactNativeConfig(projectRoot) {
function readRNConfigJSFile(projectRoot) {
const rnConfigFileName = 'react-native.config.js';

console.log(
`\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${rnConfigFileName}`,
);

const rnConfigFilePath = path.resolve(projectRoot, rnConfigFileName);

if (!fs.existsSync(rnConfigFilePath)) {
return [];
}
const rnConfig = require(rnConfigFilePath);
return require(rnConfigFilePath);
}

function findLibrariesFromReactNativeConfig(projectRoot) {
console.log(
`\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in react-native.config.js`,
);

const rnConfig = readRNConfigJSFile(projectRoot);

if (rnConfig.dependencies == null) {
return [];
Expand All @@ -289,6 +294,33 @@ function findLibrariesFromReactNativeConfig(projectRoot) {
});
}

// Function to look for libraries explicitly unlinked from the app
// through the react-native.config.js file.
// If this happens, it might be that the app does not need
// to generate code for that library as it won't be used by that platform
// @return { [libraryName: string]: [platform: string] }
function findNotLinkedLibraries(projectRoot) {
const rnConfig = readRNConfigJSFile(projectRoot);

if (rnConfig.dependencies == null) {
return {};
}

let notLinkedLibraries = {};

Object.keys(rnConfig.dependencies).forEach(name => {
const dependency = rnConfig.dependencies[name];
let notLinkedPlatforms = [];
Object.keys(dependency.platforms).forEach(platform => {
if (dependency.platforms[platform] == null) {
notLinkedPlatforms.push(platform);
}
});
notLinkedLibraries[name] = notLinkedPlatforms
});
return notLinkedLibraries;
}

function findProjectRootLibraries(pkgJson, projectRoot) {
console.log('[Codegen] Searching for codegen-enabled libraries in the app.');

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

const notLinkedLibraries = findNotLinkedLibraries(projectRoot);

for (const platform of platforms) {
const outputPath = computeOutputPath(
projectRoot,
Expand All @@ -702,7 +736,15 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {
platform,
);

const schemaInfos = generateSchemaInfos(libraries);
const schemaInfos = generateSchemaInfos(libraries.filter(library => {
const unlinkedPlatforms = notLinkedLibraries[library.libraryName];
if (unlinkedPlatforms && unlinkedPlatforms.includes(platform)) {
console.log(`[Codegen - ${library.libraryName}] Skipping Codegen on ${platform}`);
return false;
}
return true;
}));

generateNativeCode(
outputPath,
schemaInfos.filter(schemaInfo =>
Expand Down

0 comments on commit 3cedb09

Please sign in to comment.