From 142090a5f3fa7c3ab2ed4c536792e3f26582bd3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 9 Nov 2021 01:13:45 -0800 Subject: [PATCH] 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 --- .../ScrollView/__tests__/ScrollView-test.js | 12 +-- .../__tests__/Pressability-test.js | 13 +-- jest/preprocessor.js | 86 ++----------------- repo-config/package.json | 5 +- 4 files changed, 23 insertions(+), 93 deletions(-) diff --git a/Libraries/Components/ScrollView/__tests__/ScrollView-test.js b/Libraries/Components/ScrollView/__tests__/ScrollView-test.js index f2a7abe2c9f852..6800bb2a681931 100644 --- a/Libraries/Components/ScrollView/__tests__/ScrollView-test.js +++ b/Libraries/Components/ScrollView/__tests__/ScrollView-test.js @@ -11,12 +11,12 @@ 'use strict'; -import * as React from 'react'; -import ScrollView from '../ScrollView'; -import * as ReactNativeTestTools from '../../../Utilities/ReactNativeTestTools'; -import ReactTestRenderer from 'react-test-renderer'; -import View from '../../View/View'; -import Text from '../../../Text/Text'; +const React = require('react'); +const ScrollView = require('../ScrollView'); +const ReactNativeTestTools = require('../../../Utilities/ReactNativeTestTools'); +const ReactTestRenderer = require('react-test-renderer'); +const View = require('../../View/View'); +const Text = require('../../../Text/Text'); describe('', () => { it('should render as expected', () => { diff --git a/Libraries/Pressability/__tests__/Pressability-test.js b/Libraries/Pressability/__tests__/Pressability-test.js index 6960546e376a74..50172911ffcf17 100644 --- a/Libraries/Pressability/__tests__/Pressability-test.js +++ b/Libraries/Pressability/__tests__/Pressability-test.js @@ -10,12 +10,13 @@ */ import type {PressEvent} from '../../Types/CoreEventTypes'; -import * as HoverState from '../HoverState'; -import Pressability from '../Pressability'; -import invariant from 'invariant'; -import nullthrows from 'nullthrows'; -import Platform from '../../Utilities/Platform'; -import UIManager from '../../ReactNative/UIManager'; + +const HoverState = require('../HoverState'); +const Pressability = require('../Pressability').default; +const invariant = require('invariant'); +const nullthrows = require('nullthrows'); +const Platform = require('../../Utilities/Platform'); +const UIManager = require('../../ReactNative/UIManager'); // TODO: Move this util to a shared location. function getMock, TReturn>( diff --git a/jest/preprocessor.js b/jest/preprocessor.js index e209750d697334..fd365c847a6c3e 100644 --- a/jest/preprocessor.js +++ b/jest/preprocessor.js @@ -13,42 +13,10 @@ 'use strict'; const babelRegisterOnly = require('metro-babel-register'); -const nullthrows = require('nullthrows'); -const createCacheKeyFunction = require('@jest/create-cache-key-function') - .default; -const t = require('@babel/types'); -const {statements} = require('@babel/template').default; +const createCacheKeyFunction = + require('@jest/create-cache-key-function').default; -const importDefault = '__importDefault__'; -const importAll = '__importAll__'; - -// prelude -const importPrelude = statements(` - function ${importDefault}(moduleId) { - const exports = require(moduleId); - - if (exports && exports.__esModule) { - return exports.default; - } - - return exports; - }; - - function ${importAll}(moduleId) { - const exports = require(moduleId); - - if (exports && exports.__esModule) { - return exports; - } - - return Object.assign({}, exports, {default: exports}); - }; -`); - -const { - transformSync: babelTransformSync, - transformFromAstSync: babelTransformFromAstSync, -} = require('@babel/core'); +const {transformSync: babelTransformSync} = require('@babel/core'); const generate = require('@babel/generator').default; const nodeFiles = new RegExp( @@ -73,13 +41,13 @@ module.exports = { }).code; } - let {ast} = transformer.transform({ + const {ast} = transformer.transform({ filename: file, options: { ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044 dev: true, enableBabelRuntime: false, - experimentalImportSupport: true, + experimentalImportSupport: false, globalPrefix: '', hot: false, inlineRequires: true, @@ -111,6 +79,10 @@ module.exports = { [require('@babel/plugin-transform-regenerator')], [require('@babel/plugin-transform-sticky-regex')], [require('@babel/plugin-transform-unicode-regex')], + [ + require('@babel/plugin-transform-modules-commonjs'), + {strict: false, allowTopLevelThis: true}, + ], [require('@babel/plugin-transform-classes')], [require('@babel/plugin-transform-arrow-functions')], [require('@babel/plugin-transform-spread')], @@ -127,46 +99,6 @@ module.exports = { ], }); - // We're not using @babel/plugin-transform-modules-commonjs so - // we need to add 'use strict' manually - const directives = ast.program.directives; - - if ( - ast.program.sourceType === 'module' && - (directives == null || - directives.findIndex(d => d.value.value === 'use strict') === -1) - ) { - ast.program.directives = [ - ...(directives || []), - t.directive(t.directiveLiteral('use strict')), - ]; - } - - // Postprocess the transformed module to handle ESM and inline requires. - // We need to do this in a separate pass to avoid issues tracking references. - const babelTransformResult = babelTransformFromAstSync(ast, src, { - ast: true, - retainLines: true, - plugins: [ - [ - require('metro-transform-plugins').importExportPlugin, - {importDefault, importAll}, - ], - [ - require('babel-preset-fbjs/plugins/inline-requires.js'), - {inlineableCalls: [importDefault, importAll]}, - ], - ], - sourceType: 'module', - }); - - ast = nullthrows(babelTransformResult.ast); - - // Inject import helpers *after* running the inline-requires transform, - // because otherwise it will assume they are user code and bail out of - // inlining calls to them. - ast.program.body.unshift(...importPrelude()); - return generate( ast, // $FlowFixMe[prop-missing] Error found when improving flow typing for libs diff --git a/repo-config/package.json b/repo-config/package.json index 9cd9c30b9a59be..2e1b4f1f88cfb1 100644 --- a/repo-config/package.json +++ b/repo-config/package.json @@ -11,13 +11,10 @@ "dependencies": { "@babel/core": "^7.14.0", "@babel/generator": "^7.14.0", - "@babel/template": "^7.0.0", - "@babel/types": "^7.0.0", "@react-native-community/eslint-plugin": "*", "@reactions/component": "^2.0.2", "async": "^2.4.0", "babel-eslint": "^10.1.0", - "babel-preset-fbjs": "^3.4.0", "clang-format": "^1.2.4", "connect": "^3.6.5", "coveralls": "^3.0.2", @@ -40,7 +37,7 @@ "jest": "^26.6.3", "jest-junit": "^10.0.0", "jscodeshift": "^0.11.0", - "metro-transform-plugins": "^0.66.0", + "metro-babel-register": "0.66.2", "mkdirp": "^0.5.1", "prettier": "1.19.1", "react": "17.0.2",