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

Open
wants to merge 1 commit into
base: 0.76-stable
Choose a base branch
from
Open
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);
}
});
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