From f502aae2926a0d944d1034c1426640f3e4abca0a Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 21 Jan 2024 14:07:19 +0100 Subject: [PATCH] Add support for RN 73 with backwards compatibility (#45) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add support for RN 73 with backwards compatibility * fix typo --------- Co-authored-by: Lukas Müller <18392496+WookieFPV@users.noreply.github.com> --- src/cli/getAssets.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/cli/getAssets.js b/src/cli/getAssets.js index 8b3ca41..db1f403 100644 --- a/src/cli/getAssets.js +++ b/src/cli/getAssets.js @@ -2,6 +2,16 @@ const Server = require('metro/src/Server'); const { loadConfig } = require('metro-config'); const output = require('metro/src/shared/output/bundle'); +const getBabelTransformerPath = () => { + try { + // for RN 73+ + return require.resolve('@react-native/metro-babel-transformer'); + } catch (e) { + // to ensure backwards compatibility with old RN versions (RN < 73) + return require.resolve('metro-react-native-babel-transformer'); + } +} + async function getAssets(options) { const args = { entryFile: options.entryFile, @@ -22,9 +32,7 @@ async function getAssets(options) { }, transformer: { allowOptionalDependencies: true, - babelTransformerPath: require.resolve( - 'metro-react-native-babel-transformer' - ), + babelTransformerPath: getBabelTransformerPath(), assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry', }, }