Skip to content
Closed
Show file tree
Hide file tree
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 @@ -21,6 +21,7 @@ const {
const {
cleanupEmptyFilesAndFolders,
extractLibrariesFromJSON,
readReactNativeConfig,
} = require('../generate-artifacts-executor/utils');
const fs = require('node:fs');
const os = require('node:os');
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}
}
Expand Down