From b71cebbd379b50460fc7a0d0b657ac88409bcf68 Mon Sep 17 00:00:00 2001 From: Blake Friedman Date: Tue, 16 Apr 2024 07:16:48 -0700 Subject: [PATCH] react-native.config.js handle optional @react-native-community dependencies Summary: As part of decoupling our dependency on the react-native-community/cli, the `react-native.config.js` which is a part of the community's config ecosystem should probably be entirely removed from the react-native package. As part of this, we're making the config fail gracefully if these dependencies aren't available in a user's project: - react-native-community/cli-platform-android - react-native-community/cli-platform-ios Changelog: [Internal] This isn't going to be a visible change to any users. bypass-github-export-checks Reviewed By: cortinico Differential Revision: D56137820 fbshipit-source-id: 528e25809a83b90e79b806a875001fc0f06db1cf --- packages/react-native/react-native.config.js | 74 ++++++++++++++------ 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 64841ed37a7..b2dd5a5c643 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -9,8 +9,39 @@ 'use strict'; -const android = require('@react-native-community/cli-platform-android'); -const ios = require('@react-native-community/cli-platform-ios'); +// React Native shouldn't be exporting itself like this, the Community Template should be be directly +// depending on and injecting: +// - @react-native-community/cli-platform-android +// - @react-native-community/cli-platform-ios +// - @react-native/community-cli-plugin (via the @react-native/core-cli-utils package) +// - codegen command should be inhoused into @react-native-community/cli +// +// This is a temporary workaround. + +const verbose = process.env.DEBUG && process.env.DEBUG.includes('react-native'); + +let android; +try { + android = require('@react-native-community/cli-platform-android'); +} catch { + if (verbose) { + console.warn( + '@react-native-community/cli-platform-android not found, the react-native.config.js may be unusable.', + ); + } +} + +let ios; +try { + ios = require('@react-native-community/cli-platform-ios'); +} catch { + if (verbose) { + console.warn( + '@react-native-community/cli-platform-ios not found, the react-native.config.js may be unusable.', + ); + } +} + const { bundleCommand, startCommand, @@ -43,22 +74,25 @@ const codegenCommand = { ), }; -module.exports = { - commands: [ - ...ios.commands, - ...android.commands, - bundleCommand, - startCommand, - codegenCommand, - ], - platforms: { - ios: { - projectConfig: ios.projectConfig, - dependencyConfig: ios.dependencyConfig, - }, - android: { - projectConfig: android.projectConfig, - dependencyConfig: android.dependencyConfig, - }, - }, +const config = { + commands: [bundleCommand, startCommand, codegenCommand], + platforms: {}, }; + +if (ios != null) { + config.commands.push(...ios.commands); + config.platforms.ios = { + projectConfig: ios.projectConfig, + dependencyConfig: ios.dependencyConfig, + }; +} + +if (android != null) { + config.commands.push(...android.commands); + config.platforms.android = { + projectConfig: android.projectConfig, + dependencyConfig: android.dependencyConfig, + }; +} + +module.exports = config;