From ca39a11b27bf9f90c3a792974910bb9060b2990f Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Mon, 27 Nov 2023 06:57:53 -0800 Subject: [PATCH] Refactor generate-artifacts-executor.js: delete handleLibrariesFromReactNativeConfig (#41654) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41654 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`. Reviewed By: cipolleschi Differential Revision: D51308595 fbshipit-source-id: 97841a3a8c295aa717c577bb188d48373b04ba38 --- .../generate-artifacts-executor-test.js | 70 ------------------- .../codegen/generate-artifacts-executor.js | 45 ------------ 2 files changed, 115 deletions(-) diff --git a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js index f195b66f8210f3..7a85ef8e3bd1ad 100644 --- a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js +++ b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js @@ -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(); diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index d72d427c89b66a..afcad1ae7a4cc1 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -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', @@ -343,7 +301,6 @@ function findCodegenEnabledLibraries(appRootDir) { return [ ...handleReactNativeCoreLibraries(), ...handleThirdPartyLibraries(pkgJson), - ...handleLibrariesFromReactNativeConfig(appRootDir), ...handleInAppLibraries(pkgJson, appRootDir), ]; } @@ -434,7 +391,5 @@ module.exports = { execute: execute, // exported for testing purposes only: _extractLibrariesFromJSON: extractLibrariesFromJSON, - _findCodegenEnabledLibraries: findCodegenEnabledLibraries, - _generateCode: generateCode, _cleanupEmptyFilesAndFolders: cleanupEmptyFilesAndFolders, };