From bdce09d494e6b48e6ecc9a4de41f52692a6f0f61 Mon Sep 17 00:00:00 2001 From: Dui Rapeeporn Pimup Date: Wed, 2 Sep 2026 17:30:39 -0700 Subject: [PATCH] Revert D118563281: Revert D118438065: [react-native][PR] Apply preset defaults without a Babel API Differential Revision: D118563281 Original commit changeset: 78cab65f148d Original Phabricator Diff: D118438065 fbshipit-source-id: b748a2566173b4d9ac4597062a208d157680cd5a --- .../src/__tests__/transform-snapshot-test.js | 21 ++++++++++++ .../src/configs/main.js | 32 +++++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js b/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js index 29ec5148cb0..97b70c65e0d 100644 --- a/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js +++ b/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js @@ -186,6 +186,18 @@ function transformCodeWithSourceOptimization( return result?.code ?? null; } +function transformWithoutBabelApi(code: string): string | null { + const config = preset.getPreset(code, {dev: false}); + const result = babel.transformSync(code, { + ...config, + babelrc: false, + configFile: false, + filename: MOCK_FILENAME, + sourceMaps: false, + }); + return result?.code ?? null; +} + function getSnapshotPath(configName: string): string { return path.join(OUTPUT_DIR, `${configName}.js`); } @@ -282,6 +294,15 @@ describe('react-native-babel-preset transform snapshots', () => { ); describe('specific feature transformations', () => { + it('builds the default config without options or a Babel API', () => { + expect(() => preset()).not.toThrow(); + }); + + it('uses the default transform profile without a Babel API', () => { + const result = transformWithoutBabelApi('class Animal {}'); + expect(result).toContain('class Animal'); + }); + it('adds display names when React.createClass contains trivia', () => { const code = ` const Component = React /* comment */ . createClass({ diff --git a/packages/react-native-babel-preset/src/configs/main.js b/packages/react-native-babel-preset/src/configs/main.js index 970853f3c01..24d23d46af0 100644 --- a/packages/react-native-babel-preset/src/configs/main.js +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -64,15 +64,19 @@ function getInlinePlatform(caller) { const loose = true; const getPreset = (src, options, babel) => { + options = options ?? {}; + const transformProfile = - options?.unstable_transformProfile ?? babel?.caller(getTransformProfile); + options.unstable_transformProfile ?? + babel?.caller(getTransformProfile) ?? + 'hermes-stable'; - const dev = options?.dev ?? babel?.env('development') ?? false; + const dev = options.dev ?? babel?.env('development') ?? false; - const platform = options?.platform ?? babel?.caller(getPlatform); + const platform = options.platform ?? babel?.caller(getPlatform); const inlinePlatform = - options?.inlinePlatform ?? babel?.caller(getInlinePlatform) ?? false; + options.inlinePlatform ?? babel?.caller(getInlinePlatform) ?? false; // Hermes V1 uses more optimised transform profiles. There is currently no // difference between stable and canary, but canary may in future be used to @@ -96,22 +100,22 @@ const getPreset = (src, options, babel) => { // Preserve private class fields and methods if the experiment is enabled. const preserveClassPrivate = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveClassPrivate, + options.customTransformOptions?.unstable_preserveClassPrivate, ); // Preserve async/await syntax if the experiment is enabled. const preserveAsync = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveAsync, + options.customTransformOptions?.unstable_preserveAsync, ); // Preserve block scoping (let/const) if the experiment is enabled. const preserveBlockScoping = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveBlockScoping, + options.customTransformOptions?.unstable_preserveBlockScoping, ); // Preserve destructuring syntax if the experiment is enabled. const preserveDestructuring = TRUE_VALS.has( - options?.customTransformOptions?.unstable_preserveDestructuring, + options.customTransformOptions?.unstable_preserveDestructuring, ); const isNull = src == null; @@ -144,7 +148,7 @@ const getPreset = (src, options, babel) => { extraPlugins.push([require('@react-native/babel-plugin-codegen')]); } - if (!options || !options.disableImportExportTransform) { + if (!options.disableImportExportTransform) { extraPlugins.push( [require('@babel/plugin-proposal-export-default-from')], [ @@ -153,7 +157,7 @@ const getPreset = (src, options, babel) => { strict: false, strictMode: false, // prevent "use strict" injections lazy: - options && options.lazyImportExportTransform != null + options.lazyImportExportTransform != null ? options.lazyImportExportTransform : importSpecifier => lazyImports.has(importSpecifier), allowTopLevelThis: true, // dont rewrite global `this` -> `undefined` @@ -210,11 +214,11 @@ const getPreset = (src, options, babel) => { ]); } - if (options && dev && !options.disableDeepImportWarnings) { + if (dev && !options.disableDeepImportWarnings) { firstPartyPlugins.push([require('../plugin-warn-on-deep-imports.js')]); } - if (options && dev && !options.useTransformReactJSXExperimental) { + if (dev && !options.useTransformReactJSXExperimental) { extraPlugins.push([require('@babel/plugin-transform-react-jsx-source')]); extraPlugins.push([require('@babel/plugin-transform-react-jsx-self')]); } @@ -230,9 +234,9 @@ const getPreset = (src, options, babel) => { ]); } - if (!options || options.enableBabelRuntime !== false) { + if (options.enableBabelRuntime !== false) { // Allows configuring a specific runtime version to optimize output - const isVersion = typeof options?.enableBabelRuntime === 'string'; + const isVersion = typeof options.enableBabelRuntime === 'string'; extraPlugins.push([ require('@babel/plugin-transform-runtime'),