Skip to content

Commit 142090a

Browse files
rubennorteLuna Wei
authored and
Luna Wei
committed
Revert changes in RN preprocessor
Summary: Changelog: [General][Fixed] Revert changes in Jest preprocessor to fix tests in external projects Reviewed By: yungsters Differential Revision: D32250044 fbshipit-source-id: 0ed4c9f7bcfa82349b5c2ec7af2ccda970bbb0ef
1 parent f35369e commit 142090a

File tree

4 files changed

+23
-93
lines changed

4 files changed

+23
-93
lines changed

Libraries/Components/ScrollView/__tests__/ScrollView-test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
'use strict';
1313

14-
import * as React from 'react';
15-
import ScrollView from '../ScrollView';
16-
import * as ReactNativeTestTools from '../../../Utilities/ReactNativeTestTools';
17-
import ReactTestRenderer from 'react-test-renderer';
18-
import View from '../../View/View';
19-
import Text from '../../../Text/Text';
14+
const React = require('react');
15+
const ScrollView = require('../ScrollView');
16+
const ReactNativeTestTools = require('../../../Utilities/ReactNativeTestTools');
17+
const ReactTestRenderer = require('react-test-renderer');
18+
const View = require('../../View/View');
19+
const Text = require('../../../Text/Text');
2020

2121
describe('<ScrollView />', () => {
2222
it('should render as expected', () => {

Libraries/Pressability/__tests__/Pressability-test.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
*/
1111

1212
import type {PressEvent} from '../../Types/CoreEventTypes';
13-
import * as HoverState from '../HoverState';
14-
import Pressability from '../Pressability';
15-
import invariant from 'invariant';
16-
import nullthrows from 'nullthrows';
17-
import Platform from '../../Utilities/Platform';
18-
import UIManager from '../../ReactNative/UIManager';
13+
14+
const HoverState = require('../HoverState');
15+
const Pressability = require('../Pressability').default;
16+
const invariant = require('invariant');
17+
const nullthrows = require('nullthrows');
18+
const Platform = require('../../Utilities/Platform');
19+
const UIManager = require('../../ReactNative/UIManager');
1920

2021
// TODO: Move this util to a shared location.
2122
function getMock<TArguments: $ReadOnlyArray<mixed>, TReturn>(

jest/preprocessor.js

+9-77
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,10 @@
1313
'use strict';
1414

1515
const babelRegisterOnly = require('metro-babel-register');
16-
const nullthrows = require('nullthrows');
17-
const createCacheKeyFunction = require('@jest/create-cache-key-function')
18-
.default;
19-
const t = require('@babel/types');
20-
const {statements} = require('@babel/template').default;
16+
const createCacheKeyFunction =
17+
require('@jest/create-cache-key-function').default;
2118

22-
const importDefault = '__importDefault__';
23-
const importAll = '__importAll__';
24-
25-
// prelude
26-
const importPrelude = statements(`
27-
function ${importDefault}(moduleId) {
28-
const exports = require(moduleId);
29-
30-
if (exports && exports.__esModule) {
31-
return exports.default;
32-
}
33-
34-
return exports;
35-
};
36-
37-
function ${importAll}(moduleId) {
38-
const exports = require(moduleId);
39-
40-
if (exports && exports.__esModule) {
41-
return exports;
42-
}
43-
44-
return Object.assign({}, exports, {default: exports});
45-
};
46-
`);
47-
48-
const {
49-
transformSync: babelTransformSync,
50-
transformFromAstSync: babelTransformFromAstSync,
51-
} = require('@babel/core');
19+
const {transformSync: babelTransformSync} = require('@babel/core');
5220
const generate = require('@babel/generator').default;
5321

5422
const nodeFiles = new RegExp(
@@ -73,13 +41,13 @@ module.exports = {
7341
}).code;
7442
}
7543

76-
let {ast} = transformer.transform({
44+
const {ast} = transformer.transform({
7745
filename: file,
7846
options: {
7947
ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044
8048
dev: true,
8149
enableBabelRuntime: false,
82-
experimentalImportSupport: true,
50+
experimentalImportSupport: false,
8351
globalPrefix: '',
8452
hot: false,
8553
inlineRequires: true,
@@ -111,6 +79,10 @@ module.exports = {
11179
[require('@babel/plugin-transform-regenerator')],
11280
[require('@babel/plugin-transform-sticky-regex')],
11381
[require('@babel/plugin-transform-unicode-regex')],
82+
[
83+
require('@babel/plugin-transform-modules-commonjs'),
84+
{strict: false, allowTopLevelThis: true},
85+
],
11486
[require('@babel/plugin-transform-classes')],
11587
[require('@babel/plugin-transform-arrow-functions')],
11688
[require('@babel/plugin-transform-spread')],
@@ -127,46 +99,6 @@ module.exports = {
12799
],
128100
});
129101

130-
// We're not using @babel/plugin-transform-modules-commonjs so
131-
// we need to add 'use strict' manually
132-
const directives = ast.program.directives;
133-
134-
if (
135-
ast.program.sourceType === 'module' &&
136-
(directives == null ||
137-
directives.findIndex(d => d.value.value === 'use strict') === -1)
138-
) {
139-
ast.program.directives = [
140-
...(directives || []),
141-
t.directive(t.directiveLiteral('use strict')),
142-
];
143-
}
144-
145-
// Postprocess the transformed module to handle ESM and inline requires.
146-
// We need to do this in a separate pass to avoid issues tracking references.
147-
const babelTransformResult = babelTransformFromAstSync(ast, src, {
148-
ast: true,
149-
retainLines: true,
150-
plugins: [
151-
[
152-
require('metro-transform-plugins').importExportPlugin,
153-
{importDefault, importAll},
154-
],
155-
[
156-
require('babel-preset-fbjs/plugins/inline-requires.js'),
157-
{inlineableCalls: [importDefault, importAll]},
158-
],
159-
],
160-
sourceType: 'module',
161-
});
162-
163-
ast = nullthrows(babelTransformResult.ast);
164-
165-
// Inject import helpers *after* running the inline-requires transform,
166-
// because otherwise it will assume they are user code and bail out of
167-
// inlining calls to them.
168-
ast.program.body.unshift(...importPrelude());
169-
170102
return generate(
171103
ast,
172104
// $FlowFixMe[prop-missing] Error found when improving flow typing for libs

repo-config/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111
"dependencies": {
1212
"@babel/core": "^7.14.0",
1313
"@babel/generator": "^7.14.0",
14-
"@babel/template": "^7.0.0",
15-
"@babel/types": "^7.0.0",
1614
"@react-native-community/eslint-plugin": "*",
1715
"@reactions/component": "^2.0.2",
1816
"async": "^2.4.0",
1917
"babel-eslint": "^10.1.0",
20-
"babel-preset-fbjs": "^3.4.0",
2118
"clang-format": "^1.2.4",
2219
"connect": "^3.6.5",
2320
"coveralls": "^3.0.2",
@@ -40,7 +37,7 @@
4037
"jest": "^26.6.3",
4138
"jest-junit": "^10.0.0",
4239
"jscodeshift": "^0.11.0",
43-
"metro-transform-plugins": "^0.66.0",
40+
"metro-babel-register": "0.66.2",
4441
"mkdirp": "^0.5.1",
4542
"prettier": "1.19.1",
4643
"react": "17.0.2",

0 commit comments

Comments
 (0)