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 085f79b5cbcf..ad18ae5f28d0 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 @@ -21,6 +21,7 @@ const { const { cleanupEmptyFilesAndFolders, extractLibrariesFromJSON, + readReactNativeConfig, } = require('../generate-artifacts-executor/utils'); const fs = require('node:fs'); const os = require('node:os'); @@ -142,6 +143,58 @@ const packageJson = JSON.stringify({ }); }); +describe('readReactNativeConfig', () => { + const CONFIG_JS = "module.exports = {dependencies: {'from-js': {root: '/js'}}};"; + const CONFIG_CJS = "module.exports = {dependencies: {'from-cjs': {root: '/cjs'}}};"; + + function withProjectRoot(files, assertion) { + const projectRoot = fs.mkdtempSync( + path.join(os.tmpdir(), 'react-native-codegen-config-'), + ); + try { + for (const [name, contents] of Object.entries(files)) { + fs.writeFileSync(path.join(projectRoot, name), contents); + } + // baseOutputPath is a directory with no generated autolinking output, so + // resolution falls through to the react-native.config file. + assertion(readReactNativeConfig(projectRoot, projectRoot)); + } finally { + fs.rmSync(projectRoot, {recursive: true, force: true}); + } + } + + it('reads react-native.config.js', () => { + withProjectRoot({'react-native.config.js': CONFIG_JS}, config => { + expect(config.dependencies).toHaveProperty('from-js'); + }); + }); + + it('reads react-native.config.cjs when there is no .js config', () => { + withProjectRoot({'react-native.config.cjs': CONFIG_CJS}, config => { + expect(config.dependencies).toHaveProperty('from-cjs'); + }); + }); + + it('prefers react-native.config.js when both exist', () => { + withProjectRoot( + { + 'react-native.config.js': CONFIG_JS, + 'react-native.config.cjs': CONFIG_CJS, + }, + config => { + expect(config.dependencies).toHaveProperty('from-js'); + expect(config.dependencies).not.toHaveProperty('from-cjs'); + }, + ); + }); + + it('returns an empty config when neither exists', () => { + withProjectRoot({}, config => { + expect(config).toEqual({}); + }); + }); +}); + describe('extractSupportedApplePlatforms', () => { it('extracts platforms when podspec specifies object of platforms', () => { const myDependency = 'test-library'; diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js index ce8dfabb816c..299e35a9e9eb 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -126,14 +126,16 @@ function readReactNativeConfig( projectRoot, baseOutputPath, ); - const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); + const rnConfigFilePath = ['react-native.config.js', 'react-native.config.cjs'] + .map(fileName => path.resolve(projectRoot, fileName)) + .find(candidatePath => fs.existsSync(candidatePath)); if (autolinkingOutput) { return autolinkingOutput; - } else if (fs.existsSync(rnConfigFilePath)) { + } else if (rnConfigFilePath != null) { // $FlowFixMe[unsupported-syntax] return require(rnConfigFilePath); } else { - codegenLog(`Could not find React Native config at: ${rnConfigFilePath}`); + codegenLog(`Could not find React Native config in: ${projectRoot}`); return {}; } }