From b029db94aed3c7f4adba7833668ebd146fc1470e Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Fri, 6 Dec 2024 19:03:10 +0100 Subject: [PATCH] fix: fix syntax error when running the app with latest rn, running the app results in an error: `export declaration must be at top level of module` --- packages/react-native-builder-bob/babel-config.js | 11 ++++++++++- packages/react-native-builder-bob/babel-preset.js | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/react-native-builder-bob/babel-config.js b/packages/react-native-builder-bob/babel-config.js index 876849e1b..7edc1ba85 100644 --- a/packages/react-native-builder-bob/babel-config.js +++ b/packages/react-native-builder-bob/babel-config.js @@ -61,7 +61,16 @@ const getConfig = (defaultConfig, { root, pkg }) => { }, { include: path.join(root, src), - presets: [require.resolve('./babel-preset')], + presets: [ + [ + require.resolve('./babel-preset'), + { + // Let the app's preset handle the commonjs transform + // Otherwise this causes `export` statements in wrong places causing syntax error + supportsStaticESM: true, + }, + ], + ], }, ], }; diff --git a/packages/react-native-builder-bob/babel-preset.js b/packages/react-native-builder-bob/babel-preset.js index 4a239d946..8579cc87c 100644 --- a/packages/react-native-builder-bob/babel-preset.js +++ b/packages/react-native-builder-bob/babel-preset.js @@ -4,10 +4,16 @@ const browserslist = require('browserslist'); /** * Babel preset for React Native Builder Bob + * + * @param {Boolean} options.supportsStaticESM - Whether to preserve ESM imports/exports, defaults to `false` + * @param {Boolean} options.rewriteImportExtensions - Whether to rewrite import extensions to '.js', defaults to `false` + * @param {'automatic' | 'classic'} options.jsxRuntime - Which JSX runtime to use, defaults to 'automatic' */ module.exports = function (api, options, cwd) { const opt = (name) => - api.caller((caller) => (caller != null ? caller[name] : undefined)); + options[name] !== undefined + ? options[name] + : api.caller((caller) => (caller != null ? caller[name] : undefined)); const supportsStaticESM = opt('supportsStaticESM'); const rewriteImportExtensions = opt('rewriteImportExtensions');