Skip to content

Commit

Permalink
Refactor generate-artifacts-executor.js: delete handleLibrariesFromRe…
Browse files Browse the repository at this point in the history
…actNativeConfig (#41654)

Summary:

This diff removes support for defining external codegen targets in `react-native.config.js` for iOS. Now  you can simply add your external dependency to the project's `package.json` and it will be resolved as a normal Node packages.

## Motivation

The need for defining external codegen targets in `react-native.config.js` historically appeared due to limitations of how codegen searched for external dependencies. Basically we performed search only in the project directory. External dependency paths had to be listed in `react-native.config.js`.

After D51303793 has landed we don't need this any longer. We can simply rely on Node resolution to find those external dependencies.

Changelog: [iOS][Breaking] - Defining external codegen targets in `react-native.config.js` is not supported anymore. Define them as normal dependencies in `package.json`.

Differential Revision: D51308595
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Nov 27, 2023
1 parent b50a709 commit 171991f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,76 +118,6 @@ describe('extractLibrariesFromJSON', () => {
});
});

describe('findCodegenEnabledLibraries', () => {
const mock = require('mock-fs');
const {
_findCodegenEnabledLibraries: findCodegenEnabledLibraries,
} = require('../generate-artifacts-executor');

afterEach(() => {
mock.restore();
});

it('returns libraries defined in react-native.config.js', () => {
const projectDir = path.join(__dirname, '../../../../test-project');
const baseCodegenConfigFileDir = path.join(__dirname, '../../..');
const baseCodegenConfigFilePath = path.join(
baseCodegenConfigFileDir,
'package.json',
);

mock({
[baseCodegenConfigFilePath]: `
{
"codegenConfig": {}
}
`,
[projectDir]: {
app: {
'package.json': `{
"name": "my-app"
}`,
'react-native.config.js': '',
},
'library-foo': {
'package.json': `{
"name": "react-native-foo",
"codegenConfig": {
"name": "RNFooSpec",
"type": "modules",
"jsSrcsDir": "src"
}
}`,
},
},
});

jest.mock(path.join(projectDir, 'app', 'react-native.config.js'), () => ({
dependencies: {
'react-native-foo': {
root: path.join(projectDir, 'library-foo'),
},
'react-native-bar': {
root: path.join(projectDir, 'library-bar'),
},
},
}));

const libraries = findCodegenEnabledLibraries(`${projectDir}/app`);

expect(libraries).toEqual([
{
config: {},
libraryPath: baseCodegenConfigFileDir,
},
{
config: {name: 'RNFooSpec', type: 'modules', jsSrcsDir: 'src'},
libraryPath: path.join(projectDir, 'library-foo'),
},
]);
});
});

describe('delete empty files and folders', () => {
beforeEach(() => {
jest.resetModules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,48 +180,6 @@ function handleThirdPartyLibraries(pkgJson) {
});
}

function handleLibrariesFromReactNativeConfig(appRootDir) {
const rnConfigFileName = 'react-native.config.js';

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

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

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

if (rnConfig.dependencies == null) {
return [];
}
return Object.keys(rnConfig.dependencies).flatMap(name => {
const dependencyConfig = rnConfig.dependencies[name];

if (!dependencyConfig.root) {
return [];
}
const codegenConfigFileDir = path.resolve(
appRootDir,
dependencyConfig.root,
);
let configFile;
try {
configFile = readPkgJsonInDirectory(codegenConfigFileDir);
} catch {
return [];
}

return extractLibrariesFromJSON(
configFile,
configFile.name,
codegenConfigFileDir,
);
});
}

function handleInAppLibraries(pkgJson, appRootDir) {
console.log(
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app',
Expand Down Expand Up @@ -343,7 +301,6 @@ function findCodegenEnabledLibraries(appRootDir) {
return [
...handleReactNativeCoreLibraries(),
...handleThirdPartyLibraries(pkgJson),
...handleLibrariesFromReactNativeConfig(appRootDir),
...handleInAppLibraries(pkgJson, appRootDir),
];
}
Expand Down Expand Up @@ -434,7 +391,5 @@ module.exports = {
execute: execute,
// exported for testing purposes only:
_extractLibrariesFromJSON: extractLibrariesFromJSON,
_findCodegenEnabledLibraries: findCodegenEnabledLibraries,
_generateCode: generateCode,
_cleanupEmptyFilesAndFolders: cleanupEmptyFilesAndFolders,
};

0 comments on commit 171991f

Please sign in to comment.