Skip to content

Commit bdce09d

Browse files
Dui Rapeeporn Pimupmeta-codesync[bot]
authored andcommitted
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
1 parent 6306c4a commit bdce09d

2 files changed

Lines changed: 39 additions & 14 deletions

File tree

packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ function transformCodeWithSourceOptimization(
186186
return result?.code ?? null;
187187
}
188188

189+
function transformWithoutBabelApi(code: string): string | null {
190+
const config = preset.getPreset(code, {dev: false});
191+
const result = babel.transformSync(code, {
192+
...config,
193+
babelrc: false,
194+
configFile: false,
195+
filename: MOCK_FILENAME,
196+
sourceMaps: false,
197+
});
198+
return result?.code ?? null;
199+
}
200+
189201
function getSnapshotPath(configName: string): string {
190202
return path.join(OUTPUT_DIR, `${configName}.js`);
191203
}
@@ -282,6 +294,15 @@ describe('react-native-babel-preset transform snapshots', () => {
282294
);
283295

284296
describe('specific feature transformations', () => {
297+
it('builds the default config without options or a Babel API', () => {
298+
expect(() => preset()).not.toThrow();
299+
});
300+
301+
it('uses the default transform profile without a Babel API', () => {
302+
const result = transformWithoutBabelApi('class Animal {}');
303+
expect(result).toContain('class Animal');
304+
});
305+
285306
it('adds display names when React.createClass contains trivia', () => {
286307
const code = `
287308
const Component = React /* comment */ . createClass({

packages/react-native-babel-preset/src/configs/main.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,19 @@ function getInlinePlatform(caller) {
6464
const loose = true;
6565

6666
const getPreset = (src, options, babel) => {
67+
options = options ?? {};
68+
6769
const transformProfile =
68-
options?.unstable_transformProfile ?? babel?.caller(getTransformProfile);
70+
options.unstable_transformProfile ??
71+
babel?.caller(getTransformProfile) ??
72+
'hermes-stable';
6973

70-
const dev = options?.dev ?? babel?.env('development') ?? false;
74+
const dev = options.dev ?? babel?.env('development') ?? false;
7175

72-
const platform = options?.platform ?? babel?.caller(getPlatform);
76+
const platform = options.platform ?? babel?.caller(getPlatform);
7377

7478
const inlinePlatform =
75-
options?.inlinePlatform ?? babel?.caller(getInlinePlatform) ?? false;
79+
options.inlinePlatform ?? babel?.caller(getInlinePlatform) ?? false;
7680

7781
// Hermes V1 uses more optimised transform profiles. There is currently no
7882
// difference between stable and canary, but canary may in future be used to
@@ -96,22 +100,22 @@ const getPreset = (src, options, babel) => {
96100

97101
// Preserve private class fields and methods if the experiment is enabled.
98102
const preserveClassPrivate = TRUE_VALS.has(
99-
options?.customTransformOptions?.unstable_preserveClassPrivate,
103+
options.customTransformOptions?.unstable_preserveClassPrivate,
100104
);
101105

102106
// Preserve async/await syntax if the experiment is enabled.
103107
const preserveAsync = TRUE_VALS.has(
104-
options?.customTransformOptions?.unstable_preserveAsync,
108+
options.customTransformOptions?.unstable_preserveAsync,
105109
);
106110

107111
// Preserve block scoping (let/const) if the experiment is enabled.
108112
const preserveBlockScoping = TRUE_VALS.has(
109-
options?.customTransformOptions?.unstable_preserveBlockScoping,
113+
options.customTransformOptions?.unstable_preserveBlockScoping,
110114
);
111115

112116
// Preserve destructuring syntax if the experiment is enabled.
113117
const preserveDestructuring = TRUE_VALS.has(
114-
options?.customTransformOptions?.unstable_preserveDestructuring,
118+
options.customTransformOptions?.unstable_preserveDestructuring,
115119
);
116120

117121
const isNull = src == null;
@@ -144,7 +148,7 @@ const getPreset = (src, options, babel) => {
144148
extraPlugins.push([require('@react-native/babel-plugin-codegen')]);
145149
}
146150

147-
if (!options || !options.disableImportExportTransform) {
151+
if (!options.disableImportExportTransform) {
148152
extraPlugins.push(
149153
[require('@babel/plugin-proposal-export-default-from')],
150154
[
@@ -153,7 +157,7 @@ const getPreset = (src, options, babel) => {
153157
strict: false,
154158
strictMode: false, // prevent "use strict" injections
155159
lazy:
156-
options && options.lazyImportExportTransform != null
160+
options.lazyImportExportTransform != null
157161
? options.lazyImportExportTransform
158162
: importSpecifier => lazyImports.has(importSpecifier),
159163
allowTopLevelThis: true, // dont rewrite global `this` -> `undefined`
@@ -210,11 +214,11 @@ const getPreset = (src, options, babel) => {
210214
]);
211215
}
212216

213-
if (options && dev && !options.disableDeepImportWarnings) {
217+
if (dev && !options.disableDeepImportWarnings) {
214218
firstPartyPlugins.push([require('../plugin-warn-on-deep-imports.js')]);
215219
}
216220

217-
if (options && dev && !options.useTransformReactJSXExperimental) {
221+
if (dev && !options.useTransformReactJSXExperimental) {
218222
extraPlugins.push([require('@babel/plugin-transform-react-jsx-source')]);
219223
extraPlugins.push([require('@babel/plugin-transform-react-jsx-self')]);
220224
}
@@ -230,9 +234,9 @@ const getPreset = (src, options, babel) => {
230234
]);
231235
}
232236

233-
if (!options || options.enableBabelRuntime !== false) {
237+
if (options.enableBabelRuntime !== false) {
234238
// Allows configuring a specific runtime version to optimize output
235-
const isVersion = typeof options?.enableBabelRuntime === 'string';
239+
const isVersion = typeof options.enableBabelRuntime === 'string';
236240

237241
extraPlugins.push([
238242
require('@babel/plugin-transform-runtime'),

0 commit comments

Comments
 (0)