Skip to content

Commit

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

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 2afe1ee commit 3b20424
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,79 +87,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",
"dependencies": {
"react-native": "*"
}
}`,
'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 @@ -145,43 +145,6 @@ function findExternalLibraries(pkgJson) {
});
}

function findLibrariesFromReactNativeConfig(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);

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(
projectRoot,
dependencyConfig.root,
);
let configFile;
try {
configFile = readPkgJsonInDirectory(codegenConfigFileDir);
} catch {
return [];
}
return extractLibrariesFromJSON(configFile, codegenConfigFileDir);
});
}

function findProjectRootLibraries(pkgJson, projectRoot) {
console.log(
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app',
Expand Down Expand Up @@ -304,9 +267,8 @@ function createComponentProvider(schemas) {
function findCodegenEnabledLibraries(projectRoot) {
const pkgJson = readPkgJsonInDirectory(projectRoot);
return [
...findExternalLibraries(pkgJson),
...findLibrariesFromReactNativeConfig(projectRoot),
...findProjectRootLibraries(pkgJson, projectRoot),
...findExternalLibraries(pkgJson),
];
}

Expand Down Expand Up @@ -392,7 +354,5 @@ module.exports = {
execute: execute,
// exported for testing purposes only:
_extractLibrariesFromJSON: extractLibrariesFromJSON,
_findCodegenEnabledLibraries: findCodegenEnabledLibraries,
_generateCode: generateCode,
_cleanupEmptyFilesAndFolders: cleanupEmptyFilesAndFolders,
};

0 comments on commit 3b20424

Please sign in to comment.