Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down Expand Up @@ -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() {
Expand Down
32 changes: 17 additions & 15 deletions packages/react-native-babel-preset/src/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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')],
[
Expand All @@ -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`
Expand Down Expand Up @@ -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')]);
}
Expand All @@ -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'),
Expand Down
Loading