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 1e972039ee5e..dd56f5b1b5bd 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 @@ -171,6 +171,18 @@ function transformCode( 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`); } @@ -267,6 +279,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('handles async generators', () => { const code = ` async function* gen() { diff --git a/packages/react-native-babel-preset/src/configs/main.js b/packages/react-native-babel-preset/src/configs/main.js index a48b126ca8b8..028e2eb92390 100644 --- a/packages/react-native-babel-preset/src/configs/main.js +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -63,16 +63,18 @@ function getInlinePlatform(caller) { // use `this.foo = bar` instead of `this.defineProperty('foo', ...)` const loose = true; -const getPreset = (src, options, babel) => { +const getPreset = (src, options = {}, babel) => { 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 +98,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 +146,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 +155,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 +212,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 +232,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'),