From 2d67c25f85fc500fc7dc1009737ea18af23f7af4 Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Thu, 27 Jul 2023 10:01:49 -0700 Subject: [PATCH] Move react-native-babel-transformer and react-native-babel-preset from Metro to React Native repo (#1024) Summary: Pull Request resolved: https://github.com/facebook/metro/pull/1024 X-link: https://github.com/facebook/react-native/pull/38228 Changelog: [General][Changed] - Move react-native-babel-transformer and react-native-babel-preset from Metro to React Native repo. Metro Changelog: **[Breaking]** - Remove `metro-react-native-babel-transformer` and `metro-react-native-babel-preset`, to be published as `react-native/metro-babel-transformer` and `react-native/babel-preset` instead. This diff does the following: - Move `metro/packages/metro-react-native-babel-preset` to `react-native/packages/react-native-babel-preset`. - Rename `metro-react-native-babel-preset` package to `react-native/babel-preset`. - Move `metro/packages/metro-react-native-babel-transformer` to `react-native/packages/react-native-babel-transformer`. - Rename `metro-react-native-babel-transformer` package to `react-native/metro-babel-transformer`. - Upadate dependencies. Reviewed By: robhogan Differential Revision: D46977466 fbshipit-source-id: ca3ff429b971f82e6fc1099a9e9fb9e8ac76a155 --- docs/ModuleAPI.md | 2 +- .../.npmignore | 6 - .../metro-react-native-babel-preset/README.md | 41 --- .../package.json | 67 ----- .../src/configs/hmr.js | 17 -- .../src/configs/lazy-imports.js | 93 ------- .../src/configs/main.js | 215 --------------- .../src/index.js | 20 -- .../src/passthrough-syntax-plugins.js | 23 -- .../.npmignore | 6 - .../package.json | 33 --- .../src/__tests__/transform-test.js | 53 ---- .../src/index.js | 255 ------------------ .../__tests__/MetroFastRefreshMockRuntime.js | 2 +- .../src/polyfills/__tests__/require-test.js | 2 +- packages/metro-transform-worker/package.json | 2 +- .../src/__tests__/index-test.js | 3 +- packages/metro/package.json | 4 +- .../src/integration_tests/metro.config.js | 2 +- yarn.lock | 56 ++++ 20 files changed, 65 insertions(+), 837 deletions(-) delete mode 100644 packages/metro-react-native-babel-preset/.npmignore delete mode 100644 packages/metro-react-native-babel-preset/README.md delete mode 100644 packages/metro-react-native-babel-preset/package.json delete mode 100644 packages/metro-react-native-babel-preset/src/configs/hmr.js delete mode 100644 packages/metro-react-native-babel-preset/src/configs/lazy-imports.js delete mode 100644 packages/metro-react-native-babel-preset/src/configs/main.js delete mode 100644 packages/metro-react-native-babel-preset/src/index.js delete mode 100644 packages/metro-react-native-babel-preset/src/passthrough-syntax-plugins.js delete mode 100644 packages/metro-react-native-babel-transformer/.npmignore delete mode 100644 packages/metro-react-native-babel-transformer/package.json delete mode 100644 packages/metro-react-native-babel-transformer/src/__tests__/transform-test.js delete mode 100644 packages/metro-react-native-babel-transformer/src/index.js diff --git a/docs/ModuleAPI.md b/docs/ModuleAPI.md index 6dde031b31..04fa9a68dd 100644 --- a/docs/ModuleAPI.md +++ b/docs/ModuleAPI.md @@ -41,7 +41,7 @@ Similar to [`module.exports`](https://nodejs.org/api/modules.html#moduleexports) We currently recommend the use of [`@babel/plugin-transform-modules-commonjs`](https://babeljs.io/docs/babel-plugin-transform-modules-commonjs) in Metro projects to support `import` and `export`. :::note -In React Native projects that use `metro-react-native-babel-preset`, `import` and `export` are supported out of the box. +In React Native projects that use `@react-native/babel-preset`, `import` and `export` are supported out of the box. ::: ## `import()` (dynamic import) diff --git a/packages/metro-react-native-babel-preset/.npmignore b/packages/metro-react-native-babel-preset/.npmignore deleted file mode 100644 index 0ec3b99a14..0000000000 --- a/packages/metro-react-native-babel-preset/.npmignore +++ /dev/null @@ -1,6 +0,0 @@ -**/__mocks__/ -**/__tests__/ -/build/ -/src.real/ -/types/ -yarn.lock diff --git a/packages/metro-react-native-babel-preset/README.md b/packages/metro-react-native-babel-preset/README.md deleted file mode 100644 index 9f39e235c2..0000000000 --- a/packages/metro-react-native-babel-preset/README.md +++ /dev/null @@ -1,41 +0,0 @@ -# metro-react-native-babel-preset - -Babel presets for [React Native](https://reactnative.dev) applications. React Native itself uses this Babel preset by default when transforming your app's source code. - -If you wish to use a custom Babel configuration by writing a `babel.config.js` file in your project's root directory, you must specify all the plugins necessary to transform your code. React Native does not apply its default Babel configuration in this case. So, to make your life easier, you can use this preset to get the default configuration and then specify more plugins that run before it. - -## Usage - -As mentioned above, you only need to use this preset if you are writing a custom `babel.config.js` file. - -### Installation - -Install `metro-react-native-babel-preset` in your app: - -with `npm`: - -```sh -npm i metro-react-native-babel-preset --save-dev -``` - -or with `yarn`: - -```sh -yarn add -D metro-react-native-babel-preset -``` - -### Configuring Babel - -Then, create a file called `babel.config.js` in your project's root directory. The existence of this `babel.config.js` file will tell React Native to use your custom Babel configuration instead of its own. Then load this preset: - -``` -{ - "presets": ["module:metro-react-native-babel-preset"] -} -``` - -You can further customize your Babel configuration by specifying plugins and other options. See [Babel's `babel.config.js` documentation](https://babeljs.io/docs/en/config-files/) to learn more. - -## Help and Support - -If you get stuck configuring Babel, please ask a question on Stack Overflow or find a consultant for help. If you discover a bug, please open up an issue. diff --git a/packages/metro-react-native-babel-preset/package.json b/packages/metro-react-native-babel-preset/package.json deleted file mode 100644 index 82d3d27c72..0000000000 --- a/packages/metro-react-native-babel-preset/package.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "name": "metro-react-native-babel-preset", - "version": "0.77.0", - "description": "Babel preset for React Native applications", - "main": "src/index.js", - "repository": { - "type": "git", - "url": "git@github.com:facebook/metro.git" - }, - "scripts": { - "prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src", - "cleanup-release": "test ! -e build && mv src build && mv src.real src" - }, - "keywords": [ - "babel", - "preset", - "react-native" - ], - "license": "MIT", - "dependencies": { - "@babel/core": "^7.20.0", - "@babel/plugin-proposal-async-generator-functions": "^7.0.0", - "@babel/plugin-proposal-class-properties": "^7.18.0", - "@babel/plugin-proposal-export-default-from": "^7.0.0", - "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.0", - "@babel/plugin-proposal-numeric-separator": "^7.0.0", - "@babel/plugin-proposal-object-rest-spread": "^7.20.0", - "@babel/plugin-proposal-optional-catch-binding": "^7.0.0", - "@babel/plugin-proposal-optional-chaining": "^7.20.0", - "@babel/plugin-syntax-dynamic-import": "^7.8.0", - "@babel/plugin-syntax-export-default-from": "^7.0.0", - "@babel/plugin-syntax-flow": "^7.18.0", - "@babel/plugin-syntax-nullish-coalescing-operator": "^7.0.0", - "@babel/plugin-syntax-optional-chaining": "^7.0.0", - "@babel/plugin-transform-arrow-functions": "^7.0.0", - "@babel/plugin-transform-async-to-generator": "^7.20.0", - "@babel/plugin-transform-block-scoping": "^7.0.0", - "@babel/plugin-transform-classes": "^7.0.0", - "@babel/plugin-transform-computed-properties": "^7.0.0", - "@babel/plugin-transform-destructuring": "^7.20.0", - "@babel/plugin-transform-flow-strip-types": "^7.20.0", - "@babel/plugin-transform-function-name": "^7.0.0", - "@babel/plugin-transform-literals": "^7.0.0", - "@babel/plugin-transform-modules-commonjs": "^7.0.0", - "@babel/plugin-transform-named-capturing-groups-regex": "^7.0.0", - "@babel/plugin-transform-parameters": "^7.0.0", - "@babel/plugin-transform-react-display-name": "^7.0.0", - "@babel/plugin-transform-react-jsx": "^7.0.0", - "@babel/plugin-transform-react-jsx-self": "^7.0.0", - "@babel/plugin-transform-react-jsx-source": "^7.0.0", - "@babel/plugin-transform-runtime": "^7.0.0", - "@babel/plugin-transform-shorthand-properties": "^7.0.0", - "@babel/plugin-transform-spread": "^7.0.0", - "@babel/plugin-transform-sticky-regex": "^7.0.0", - "@babel/plugin-transform-typescript": "^7.5.0", - "@babel/plugin-transform-unicode-regex": "^7.0.0", - "@babel/template": "^7.0.0", - "babel-plugin-transform-flow-enums": "^0.0.2", - "react-refresh": "^0.4.0" - }, - "peerDependencies": { - "@babel/core": "*" - }, - "engines": { - "node": ">=18" - } -} diff --git a/packages/metro-react-native-babel-preset/src/configs/hmr.js b/packages/metro-react-native-babel-preset/src/configs/hmr.js deleted file mode 100644 index 46cbcbd485..0000000000 --- a/packages/metro-react-native-babel-preset/src/configs/hmr.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @oncall react_native - */ - -'use strict'; - -module.exports = function () { - return { - plugins: [require('react-refresh/babel')], - }; -}; diff --git a/packages/metro-react-native-babel-preset/src/configs/lazy-imports.js b/packages/metro-react-native-babel-preset/src/configs/lazy-imports.js deleted file mode 100644 index 1c001f67a9..0000000000 --- a/packages/metro-react-native-babel-preset/src/configs/lazy-imports.js +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @oncall react_native - */ - -// This is the set of modules that React Native publicly exports and that we -// want to require lazily. Keep this list in sync with -// react-native/index.js (though having extra entries here is fairly harmless). - -'use strict'; - -module.exports = new Set([ - 'AccessibilityInfo', - 'ActivityIndicator', - 'Button', - 'DatePickerIOS', - 'DrawerLayoutAndroid', - 'FlatList', - 'Image', - 'ImageBackground', - 'InputAccessoryView', - 'KeyboardAvoidingView', - 'Modal', - 'Pressable', - 'ProgressBarAndroid', - 'ProgressViewIOS', - 'SafeAreaView', - 'ScrollView', - 'SectionList', - 'Slider', - 'Switch', - 'RefreshControl', - 'StatusBar', - 'Text', - 'TextInput', - 'Touchable', - 'TouchableHighlight', - 'TouchableNativeFeedback', - 'TouchableOpacity', - 'TouchableWithoutFeedback', - 'View', - 'VirtualizedList', - 'VirtualizedSectionList', - - // APIs - 'ActionSheetIOS', - 'Alert', - 'Animated', - 'Appearance', - 'AppRegistry', - 'AppState', - 'AsyncStorage', - 'BackHandler', - 'Clipboard', - 'DeviceInfo', - 'Dimensions', - 'Easing', - 'ReactNative', - 'I18nManager', - 'InteractionManager', - 'Keyboard', - 'LayoutAnimation', - 'Linking', - 'LogBox', - 'NativeEventEmitter', - 'PanResponder', - 'PermissionsAndroid', - 'PixelRatio', - 'PushNotificationIOS', - 'Settings', - 'Share', - 'StyleSheet', - 'Systrace', - 'ToastAndroid', - 'TVEventHandler', - 'UIManager', - 'ReactNative', - 'UTFSequence', - 'Vibration', - - // Plugins - 'RCTDeviceEventEmitter', - 'RCTNativeAppEventEmitter', - 'NativeModules', - 'Platform', - 'processColor', - 'requireNativeComponent', -]); diff --git a/packages/metro-react-native-babel-preset/src/configs/main.js b/packages/metro-react-native-babel-preset/src/configs/main.js deleted file mode 100644 index 675d51de5f..0000000000 --- a/packages/metro-react-native-babel-preset/src/configs/main.js +++ /dev/null @@ -1,215 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @oncall react_native - */ - -'use strict'; - -const passthroughSyntaxPlugins = require('../passthrough-syntax-plugins'); -const lazyImports = require('./lazy-imports'); - -function isTypeScriptSource(fileName) { - return !!fileName && fileName.endsWith('.ts'); -} - -function isTSXSource(fileName) { - return !!fileName && fileName.endsWith('.tsx'); -} - -const defaultPlugins = [ - [require('@babel/plugin-syntax-flow')], - [require('babel-plugin-transform-flow-enums')], - [require('@babel/plugin-transform-block-scoping')], - [ - require('@babel/plugin-proposal-class-properties'), - // use `this.foo = bar` instead of `this.defineProperty('foo', ...)` - {loose: true}, - ], - [require('@babel/plugin-syntax-dynamic-import')], - [require('@babel/plugin-syntax-export-default-from')], - ...passthroughSyntaxPlugins, - [require('@babel/plugin-transform-unicode-regex')], -]; - -const getPreset = (src, options) => { - const transformProfile = - (options && options.unstable_transformProfile) || 'default'; - const isHermesStable = transformProfile === 'hermes-stable'; - const isHermesCanary = transformProfile === 'hermes-canary'; - const isHermes = isHermesStable || isHermesCanary; - - const isNull = src == null; - const hasClass = isNull || src.indexOf('class') !== -1; - - const extraPlugins = []; - if (!options.useTransformReactJSXExperimental) { - extraPlugins.push([ - require('@babel/plugin-transform-react-jsx'), - {runtime: 'automatic'}, - ]); - } - - if (!options || !options.disableImportExportTransform) { - extraPlugins.push( - [require('@babel/plugin-proposal-export-default-from')], - [ - require('@babel/plugin-transform-modules-commonjs'), - { - strict: false, - strictMode: false, // prevent "use strict" injections - lazy: - options && options.lazyImportExportTransform != null - ? options.lazyImportExportTransform - : importSpecifier => lazyImports.has(importSpecifier), - allowTopLevelThis: true, // dont rewrite global `this` -> `undefined` - }, - ], - ); - } - - if (hasClass) { - extraPlugins.push([require('@babel/plugin-transform-classes')]); - } - - // TODO(gaearon): put this back into '=>' indexOf bailout - // and patch react-refresh to not depend on this transform. - extraPlugins.push([require('@babel/plugin-transform-arrow-functions')]); - - if (!isHermes) { - extraPlugins.push([require('@babel/plugin-transform-computed-properties')]); - extraPlugins.push([require('@babel/plugin-transform-parameters')]); - extraPlugins.push([ - require('@babel/plugin-transform-shorthand-properties'), - ]); - extraPlugins.push([ - require('@babel/plugin-proposal-optional-catch-binding'), - ]); - extraPlugins.push([require('@babel/plugin-transform-function-name')]); - extraPlugins.push([require('@babel/plugin-transform-literals')]); - extraPlugins.push([require('@babel/plugin-proposal-numeric-separator')]); - extraPlugins.push([require('@babel/plugin-transform-sticky-regex')]); - } else { - extraPlugins.push([ - require('@babel/plugin-transform-named-capturing-groups-regex'), - ]); - } - if (!isHermesCanary) { - extraPlugins.push([ - require('@babel/plugin-transform-destructuring'), - {useBuiltIns: true}, - ]); - } - if (!isHermes && (isNull || hasClass || src.indexOf('...') !== -1)) { - extraPlugins.push( - [require('@babel/plugin-transform-spread')], - [ - require('@babel/plugin-proposal-object-rest-spread'), - // Assume no dependence on getters or evaluation order. See https://github.com/babel/babel/pull/11520 - {loose: true, useBuiltIns: true}, - ], - ); - } - if (isNull || src.indexOf('async') !== -1) { - extraPlugins.push([ - require('@babel/plugin-proposal-async-generator-functions'), - ]); - extraPlugins.push([require('@babel/plugin-transform-async-to-generator')]); - } - if ( - isNull || - src.indexOf('React.createClass') !== -1 || - src.indexOf('createReactClass') !== -1 - ) { - extraPlugins.push([require('@babel/plugin-transform-react-display-name')]); - } - if (!isHermes && (isNull || src.indexOf('?.') !== -1)) { - extraPlugins.push([ - require('@babel/plugin-proposal-optional-chaining'), - {loose: true}, - ]); - } - if (!isHermes && (isNull || src.indexOf('??') !== -1)) { - extraPlugins.push([ - require('@babel/plugin-proposal-nullish-coalescing-operator'), - {loose: true}, - ]); - } - - if (options && options.dev && !options.useTransformReactJSXExperimental) { - extraPlugins.push([require('@babel/plugin-transform-react-jsx-source')]); - extraPlugins.push([require('@babel/plugin-transform-react-jsx-self')]); - } - - if (!options || options.enableBabelRuntime !== false) { - // Allows configuring a specific runtime version to optimize output - const isVersion = typeof options?.enableBabelRuntime === 'string'; - - extraPlugins.push([ - require('@babel/plugin-transform-runtime'), - { - helpers: true, - regenerator: !isHermes, - ...(isVersion && {version: options.enableBabelRuntime}), - }, - ]); - } - - return { - comments: false, - compact: true, - overrides: [ - // the flow strip types plugin must go BEFORE class properties! - // there'll be a test case that fails if you don't. - { - plugins: [require('@babel/plugin-transform-flow-strip-types')], - }, - { - plugins: defaultPlugins, - }, - { - test: isTypeScriptSource, - plugins: [ - [ - require('@babel/plugin-transform-typescript'), - { - isTSX: false, - allowNamespaces: true, - }, - ], - ], - }, - { - test: isTSXSource, - plugins: [ - [ - require('@babel/plugin-transform-typescript'), - { - isTSX: true, - allowNamespaces: true, - }, - ], - ], - }, - { - plugins: extraPlugins, - }, - ], - }; -}; - -module.exports = options => { - if (options.withDevTools == null) { - const env = process.env.BABEL_ENV || process.env.NODE_ENV; - if (!env || env === 'development') { - return getPreset(null, {...options, dev: true}); - } - } - return getPreset(null, options); -}; - -module.exports.getPreset = getPreset; diff --git a/packages/metro-react-native-babel-preset/src/index.js b/packages/metro-react-native-babel-preset/src/index.js deleted file mode 100644 index 011dfe0640..0000000000 --- a/packages/metro-react-native-babel-preset/src/index.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @oncall react_native - */ - -'use strict'; - -const main = require('./configs/main'); - -module.exports = function (babel, options) { - return main(options); -}; - -module.exports.getPreset = main.getPreset; -module.exports.passthroughSyntaxPlugins = require('./passthrough-syntax-plugins'); diff --git a/packages/metro-react-native-babel-preset/src/passthrough-syntax-plugins.js b/packages/metro-react-native-babel-preset/src/passthrough-syntax-plugins.js deleted file mode 100644 index f66f225c64..0000000000 --- a/packages/metro-react-native-babel-preset/src/passthrough-syntax-plugins.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @oncall react_native - */ - -'use strict'; - -// This list of syntax plugins is used for two purposes: -// 1. Enabling experimental syntax features in the INPUT to the Metro Babel -// transformer, regardless of whether we actually transform them. -// 2. Enabling these same features in parser passes that consume the OUTPUT of -// the Metro Babel transformer. -const passthroughSyntaxPlugins = [ - [require('@babel/plugin-syntax-nullish-coalescing-operator')], - [require('@babel/plugin-syntax-optional-chaining')], -]; - -module.exports = passthroughSyntaxPlugins; diff --git a/packages/metro-react-native-babel-transformer/.npmignore b/packages/metro-react-native-babel-transformer/.npmignore deleted file mode 100644 index 0ec3b99a14..0000000000 --- a/packages/metro-react-native-babel-transformer/.npmignore +++ /dev/null @@ -1,6 +0,0 @@ -**/__mocks__/ -**/__tests__/ -/build/ -/src.real/ -/types/ -yarn.lock diff --git a/packages/metro-react-native-babel-transformer/package.json b/packages/metro-react-native-babel-transformer/package.json deleted file mode 100644 index aab002047d..0000000000 --- a/packages/metro-react-native-babel-transformer/package.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "name": "metro-react-native-babel-transformer", - "version": "0.77.0", - "description": "Babel transformer for React Native applications.", - "main": "src/index.js", - "repository": { - "type": "git", - "url": "git@github.com:facebook/metro.git" - }, - "scripts": { - "prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src", - "cleanup-release": "test ! -e build && mv src build && mv src.real src" - }, - "keywords": [ - "transformer", - "react-native", - "metro" - ], - "license": "MIT", - "dependencies": { - "@babel/core": "^7.20.0", - "babel-preset-fbjs": "^3.4.0", - "hermes-parser": "0.15.0", - "metro-react-native-babel-preset": "0.77.0", - "nullthrows": "^1.1.1" - }, - "peerDependencies": { - "@babel/core": "*" - }, - "engines": { - "node": ">=18" - } -} diff --git a/packages/metro-react-native-babel-transformer/src/__tests__/transform-test.js b/packages/metro-react-native-babel-transformer/src/__tests__/transform-test.js deleted file mode 100644 index 3919cbfa29..0000000000 --- a/packages/metro-react-native-babel-transformer/src/__tests__/transform-test.js +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict-local - * @format - */ - -'use strict'; - -const {transform} = require('../index.js'); -const path = require('path'); - -const PROJECT_ROOT = path.sep === '/' ? '/my/project' : 'C:\\my\\project'; - -it('exposes the correct absolute path to a source file to plugins', () => { - let visitorFilename; - let pluginCwd; - transform({ - filename: 'foo.js', - src: 'console.log("foo");', - plugins: [ - (babel, opts, cwd) => { - pluginCwd = cwd; - return { - visitor: { - CallExpression: { - enter: (_, state) => { - visitorFilename = state.filename; - }, - }, - }, - }; - }, - ], - options: { - dev: true, - enableBabelRuntime: false, - enableBabelRCLookup: false, - globalPrefix: '__metro__', - hot: false, - inlineRequires: false, - minify: false, - platform: null, - publicPath: 'test', - projectRoot: PROJECT_ROOT, - }, - }); - expect(pluginCwd).toEqual(PROJECT_ROOT); - expect(visitorFilename).toEqual(path.resolve(PROJECT_ROOT, 'foo.js')); -}); diff --git a/packages/metro-react-native-babel-transformer/src/index.js b/packages/metro-react-native-babel-transformer/src/index.js deleted file mode 100644 index 80384e937a..0000000000 --- a/packages/metro-react-native-babel-transformer/src/index.js +++ /dev/null @@ -1,255 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - * @oncall react_native - */ - -// This file uses Flow comment syntax so that it may be used from source as a -// transformer without itself requiring transformation, such as in -// facebook/react-native's own tests. - -'use strict'; - -/*:: -import type {BabelCoreOptions, Plugins, TransformResult} from '@babel/core'; -import type { - BabelTransformer, - MetroBabelFileMetadata, -} from 'metro-babel-transformer'; -*/ - -const {parseSync, transformFromAstSync} = require('@babel/core'); -const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires'); -const crypto = require('crypto'); -const fs = require('fs'); -const makeHMRConfig = require('metro-react-native-babel-preset/src/configs/hmr'); -const nullthrows = require('nullthrows'); -const path = require('path'); - -const cacheKeyParts = [ - fs.readFileSync(__filename), - require('babel-preset-fbjs/package.json').version, -]; - -// TS detection conditions copied from metro-react-native-babel-preset -function isTypeScriptSource(fileName /*: string */) { - return !!fileName && fileName.endsWith('.ts'); -} - -function isTSXSource(fileName /*: string */) { - return !!fileName && fileName.endsWith('.tsx'); -} - -/** - * Return a memoized function that checks for the existence of a - * project level .babelrc file, and if it doesn't exist, reads the - * default RN babelrc file and uses that. - */ -const getBabelRC = (function () { - let babelRC /*: ?BabelCoreOptions */ = null; - - /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's - * LTI update could not be added via codemod */ - return function _getBabelRC({ - projectRoot, - extendsBabelConfigPath, - ...options - }) { - if (babelRC != null) { - return babelRC; - } - - babelRC = { - plugins: [], - extends: extendsBabelConfigPath, - }; - - if (extendsBabelConfigPath) { - return babelRC; - } - - // Let's look for a babel config file in the project root. - let projectBabelRCPath; - - // .babelrc - if (projectRoot) { - projectBabelRCPath = path.resolve(projectRoot, '.babelrc'); - } - - if (projectBabelRCPath) { - // .babelrc.js - if (!fs.existsSync(projectBabelRCPath)) { - projectBabelRCPath = path.resolve(projectRoot, '.babelrc.js'); - } - - // babel.config.js - if (!fs.existsSync(projectBabelRCPath)) { - projectBabelRCPath = path.resolve(projectRoot, 'babel.config.js'); - } - - // If we found a babel config file, extend our config off of it - // otherwise the default config will be used - if (fs.existsSync(projectBabelRCPath)) { - // $FlowFixMe[incompatible-use] `extends` is missing in null or undefined. - babelRC.extends = projectBabelRCPath; - } - } - - // If a babel config file doesn't exist in the project then - // the default preset for react-native will be used instead. - // $FlowFixMe[incompatible-use] `extends` is missing in null or undefined. - // $FlowFixMe[incompatible-type] `extends` is missing in null or undefined. - if (!babelRC.extends) { - const {experimentalImportSupport, ...presetOptions} = options; - - // $FlowFixMe[incompatible-use] `presets` is missing in null or undefined. - babelRC.presets = [ - [ - require('metro-react-native-babel-preset'), - { - projectRoot, - ...presetOptions, - disableImportExportTransform: experimentalImportSupport, - enableBabelRuntime: options.enableBabelRuntime, - }, - ], - ]; - } - - return babelRC; - }; -})(); - -/** - * Given a filename and options, build a Babel - * config object with the appropriate plugins. - */ -function buildBabelConfig( - filename /*: string */, - /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's - * LTI update could not be added via codemod */ - options, - plugins /*:: ?: Plugins*/ = [], -) /*: BabelCoreOptions*/ { - const babelRC = getBabelRC(options); - - const extraConfig /*: BabelCoreOptions */ = { - babelrc: - typeof options.enableBabelRCLookup === 'boolean' - ? options.enableBabelRCLookup - : true, - code: false, - cwd: options.projectRoot, - filename, - highlightCode: true, - }; - - let config /*: BabelCoreOptions */ = { - ...babelRC, - ...extraConfig, - }; - - // Add extra plugins - const extraPlugins = []; - - if (options.inlineRequires) { - extraPlugins.push(inlineRequiresPlugin); - } - - const withExtrPlugins = (config.plugins = extraPlugins.concat( - config.plugins, - plugins, - )); - - if (options.dev && options.hot) { - // Note: this intentionally doesn't include the path separator because - // I'm not sure which one it should use on Windows, and false positives - // are unlikely anyway. If you later decide to include the separator, - // don't forget that the string usually *starts* with "node_modules" so - // the first one often won't be there. - const mayContainEditableReactComponents = - filename.indexOf('node_modules') === -1; - - if (mayContainEditableReactComponents) { - const hmrConfig = makeHMRConfig(); - hmrConfig.plugins = withExtrPlugins.concat(hmrConfig.plugins); - config = {...config, ...hmrConfig}; - } - } - - return { - ...babelRC, - ...config, - }; -} - -const transform /*: BabelTransformer['transform'] */ = ({ - filename, - options, - src, - plugins, -}) => { - const OLD_BABEL_ENV = process.env.BABEL_ENV; - process.env.BABEL_ENV = options.dev - ? 'development' - : process.env.BABEL_ENV || 'production'; - - try { - const babelConfig = { - // ES modules require sourceType='module' but OSS may not always want that - sourceType: 'unambiguous', - ...buildBabelConfig(filename, options, plugins), - caller: {name: 'metro', bundler: 'metro', platform: options.platform}, - ast: true, - - // NOTE(EvanBacon): We split the parse/transform steps up to accommodate - // Hermes parsing, but this defaults to cloning the AST which increases - // the transformation time by a fair amount. - // You get this behavior by default when using Babel's `transform` method directly. - cloneInputAst: false, - }; - const sourceAst = - isTypeScriptSource(filename) || - isTSXSource(filename) || - !options.hermesParser - ? parseSync(src, babelConfig) - : require('hermes-parser').parse(src, { - babel: true, - sourceType: babelConfig.sourceType, - }); - - const result /*: TransformResult */ = - transformFromAstSync(sourceAst, src, babelConfig); - - // The result from `transformFromAstSync` can be null (if the file is ignored) - if (!result) { - /* $FlowFixMe BabelTransformer specifies that the `ast` can never be null but - * the function returns here. Discovered when typing `BabelNode`. */ - return {ast: null}; - } - - return {ast: nullthrows(result.ast), metadata: result.metadata}; - } finally { - if (OLD_BABEL_ENV) { - process.env.BABEL_ENV = OLD_BABEL_ENV; - } - } -}; - -function getCacheKey() { - var key = crypto.createHash('md5'); - cacheKeyParts.forEach(part => key.update(part)); - return key.digest('hex'); -} - -const babelTransformer /*: BabelTransformer */ = { - transform, - getCacheKey, -}; - -module.exports = babelTransformer; diff --git a/packages/metro-runtime/src/polyfills/__tests__/MetroFastRefreshMockRuntime.js b/packages/metro-runtime/src/polyfills/__tests__/MetroFastRefreshMockRuntime.js index 0744f2f2d0..ea2f55ab28 100644 --- a/packages/metro-runtime/src/polyfills/__tests__/MetroFastRefreshMockRuntime.js +++ b/packages/metro-runtime/src/polyfills/__tests__/MetroFastRefreshMockRuntime.js @@ -135,7 +135,7 @@ const moduleSystemCode = (() => { babelrc: false, cwd: '/', filename: 'test.js', - presets: [require.resolve('metro-react-native-babel-preset')], + presets: [require.resolve('@react-native/babel-preset')], retainLines: true, sourceMaps: 'inline', sourceType: 'module', diff --git a/packages/metro-runtime/src/polyfills/__tests__/require-test.js b/packages/metro-runtime/src/polyfills/__tests__/require-test.js index abfdcde4a0..eb9b0ecfbb 100644 --- a/packages/metro-runtime/src/polyfills/__tests__/require-test.js +++ b/packages/metro-runtime/src/polyfills/__tests__/require-test.js @@ -39,7 +39,7 @@ describe('require', () => { babelrc: false, cwd: '/', filename: 'test.js', - presets: [require.resolve('metro-react-native-babel-preset')], + presets: [require.resolve('@react-native/babel-preset')], retainLines: true, sourceMaps: 'inline', sourceType: 'module', diff --git a/packages/metro-transform-worker/package.json b/packages/metro-transform-worker/package.json index 78eba0df7b..991215be11 100644 --- a/packages/metro-transform-worker/package.json +++ b/packages/metro-transform-worker/package.json @@ -29,7 +29,7 @@ "devDependencies": { "metro-memory-fs": "0.77.0", "metro-minify-terser": "0.77.0", - "metro-react-native-babel-transformer": "0.77.0" + "@react-native/metro-babel-transformer": "0.73.11" }, "engines": { "node": ">=18" diff --git a/packages/metro-transform-worker/src/__tests__/index-test.js b/packages/metro-transform-worker/src/__tests__/index-test.js index 61399ed10a..40bfcb1d15 100644 --- a/packages/metro-transform-worker/src/__tests__/index-test.js +++ b/packages/metro-transform-worker/src/__tests__/index-test.js @@ -36,8 +36,9 @@ const {Buffer} = require('buffer'); const path = require('path'); const babelTransformerPath = require.resolve( - 'metro-react-native-babel-transformer', + '@react-native/metro-babel-transformer', ); + const transformerContents = (() => require('fs').readFileSync(babelTransformerPath))(); diff --git a/packages/metro/package.json b/packages/metro/package.json index 8def63d0dc..645debdb8c 100644 --- a/packages/metro/package.json +++ b/packages/metro/package.json @@ -62,14 +62,14 @@ }, "devDependencies": { "@babel/plugin-transform-flow-strip-types": "^7.20.0", + "@react-native/babel-preset": "0.73.15", + "@react-native/metro-babel-transformer": "0.73.11", "babel-jest": "^29.2.1", "dedent": "^0.7.0", "jest-snapshot": "^26.5.2", "jest-snapshot-serializer-raw": "^1.2.0", "metro-babel-register": "0.77.0", "metro-memory-fs": "0.77.0", - "metro-react-native-babel-preset": "0.77.0", - "metro-react-native-babel-transformer": "0.77.0", "mock-req": "^0.2.0", "mock-res": "^0.6.0", "stack-trace": "^0.0.10" diff --git a/packages/metro/src/integration_tests/metro.config.js b/packages/metro/src/integration_tests/metro.config.js index 0527b954ad..d6f16eb054 100644 --- a/packages/metro/src/integration_tests/metro.config.js +++ b/packages/metro/src/integration_tests/metro.config.js @@ -30,7 +30,7 @@ module.exports = { 'metro-runtime/src/modules/asyncRequire', ), babelTransformerPath: require.resolve( - 'metro-react-native-babel-transformer', + '@react-native/metro-babel-transformer', ), enableBabelRCLookup: false, enableBabelRuntime: false, diff --git a/yarn.lock b/yarn.lock index 78187a3227..21ef8242c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1207,6 +1207,62 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" +"@react-native/babel-preset@*", "@react-native/babel-preset@0.73.15": + version "0.73.15" + resolved "https://registry.yarnpkg.com/@react-native/babel-preset/-/babel-preset-0.73.15.tgz#c9f16db506fffc950bfca791dd074095c6e5efaf" + integrity sha512-nuWsQ/82g7c8oRWBLS5HM1juxUCS9sNlUEq40ovFpYeqpR/hfyClQ/GrfupStpX2lG1pp02AhNmcStqF85RLAw== + dependencies: + "@babel/core" "^7.20.0" + "@babel/plugin-proposal-async-generator-functions" "^7.0.0" + "@babel/plugin-proposal-class-properties" "^7.18.0" + "@babel/plugin-proposal-export-default-from" "^7.0.0" + "@babel/plugin-proposal-nullish-coalescing-operator" "^7.18.0" + "@babel/plugin-proposal-numeric-separator" "^7.0.0" + "@babel/plugin-proposal-object-rest-spread" "^7.20.0" + "@babel/plugin-proposal-optional-catch-binding" "^7.0.0" + "@babel/plugin-proposal-optional-chaining" "^7.20.0" + "@babel/plugin-syntax-dynamic-import" "^7.8.0" + "@babel/plugin-syntax-export-default-from" "^7.0.0" + "@babel/plugin-syntax-flow" "^7.18.0" + "@babel/plugin-syntax-nullish-coalescing-operator" "^7.0.0" + "@babel/plugin-syntax-optional-chaining" "^7.0.0" + "@babel/plugin-transform-arrow-functions" "^7.0.0" + "@babel/plugin-transform-async-to-generator" "^7.20.0" + "@babel/plugin-transform-block-scoping" "^7.0.0" + "@babel/plugin-transform-classes" "^7.0.0" + "@babel/plugin-transform-computed-properties" "^7.0.0" + "@babel/plugin-transform-destructuring" "^7.20.0" + "@babel/plugin-transform-flow-strip-types" "^7.20.0" + "@babel/plugin-transform-function-name" "^7.0.0" + "@babel/plugin-transform-literals" "^7.0.0" + "@babel/plugin-transform-modules-commonjs" "^7.0.0" + "@babel/plugin-transform-named-capturing-groups-regex" "^7.0.0" + "@babel/plugin-transform-parameters" "^7.0.0" + "@babel/plugin-transform-react-display-name" "^7.0.0" + "@babel/plugin-transform-react-jsx" "^7.0.0" + "@babel/plugin-transform-react-jsx-self" "^7.0.0" + "@babel/plugin-transform-react-jsx-source" "^7.0.0" + "@babel/plugin-transform-runtime" "^7.0.0" + "@babel/plugin-transform-shorthand-properties" "^7.0.0" + "@babel/plugin-transform-spread" "^7.0.0" + "@babel/plugin-transform-sticky-regex" "^7.0.0" + "@babel/plugin-transform-typescript" "^7.5.0" + "@babel/plugin-transform-unicode-regex" "^7.0.0" + "@babel/template" "^7.0.0" + babel-plugin-transform-flow-enums "^0.0.2" + react-refresh "^0.4.0" + +"@react-native/metro-babel-transformer@0.73.11": + version "0.73.11" + resolved "https://registry.yarnpkg.com/@react-native/metro-babel-transformer/-/metro-babel-transformer-0.73.11.tgz#b3b627cc8d5f2247827d4e980b5098cb2fac0606" + integrity sha512-5/8ZInljnuytOOitbZdFy93ouY9wyXip+TgMTEjj4D0Lpoyhn0pEggq65urAGPT8PqokeaVu/Z2h99CxRy9EuA== + dependencies: + "@babel/core" "^7.20.0" + "@react-native/babel-preset" "*" + babel-preset-fbjs "^3.4.0" + hermes-parser "0.15.0" + nullthrows "^1.1.1" + "@sinclair/typebox@^0.24.1": version "0.24.41" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.41.tgz#45470b8bae32a28f1e0501066d0bacbd8b772804"