Skip to content
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

[Codegen] Exclude unlinked libs from codegen #47712

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
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
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);
}
});
Comment on lines +314 to +318
Copy link
Contributor

Choose a reason for hiding this comment

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

Previously this was sufficient for module to be linked:

  dependencies: {
    'name': {
      root: '/path/to/your/module',
    },
  },

With this change, projects with this config crash during pod install since the platforms field isn't present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for raising this other case. I believe that if there is just the path, it links for all the platforms, right?

Copy link
Contributor

@satya164 satya164 Dec 9, 2024

Choose a reason for hiding this comment

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

@cipolleschi currently it crashes when dependency.platforms isn't specified because the above check is accessing keys on dependency.platforms unconditionally

current workaround is to specify:

  dependencies: {
    'name': {
      root: '/path/to/your/module',
      platforms: { android: {}, ios: {} },
    },
  },

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
Loading