Skip to content

Commit

Permalink
Move react-native-babel-transformer and react-native-babel-preset fro…
Browse files Browse the repository at this point in the history
…m Metro to React Native repo (#38228)

Summary:
X-link: facebook/metro#1024

Pull Request resolved: #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: 32478f63a0442b61a1804f12ef814c8b29d7f8bb
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Jul 27, 2023
1 parent 75b8b10 commit d380bb8
Show file tree
Hide file tree
Showing 21 changed files with 832 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": [
"module:metro-react-native-babel-preset"
"module:@react-native/babel-preset"
],
"plugins": [
"babel-plugin-transform-flow-enums"
Expand Down
4 changes: 2 additions & 2 deletions jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const nodeFiles = /[\\/]metro(?:-[^/]*)[\\/]/;
// hook. This is used below to configure babelTransformSync under Jest.
const {only: _, ...nodeBabelOptions} = metroBabelRegister.config([]);

const transformer = require('metro-react-native-babel-transformer');
const transformer = require('@react-native/metro-babel-transformer');

module.exports = {
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ {
Expand Down Expand Up @@ -82,7 +82,7 @@ module.exports = {
// $FlowFixMe[signature-verification-failure]
getCacheKey: createCacheKeyFunction([
__filename,
require.resolve('metro-react-native-babel-transformer'),
require.resolve('@react-native/metro-babel-transformer'),
require.resolve('@babel/core/package.json'),
]),
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@babel/plugin-transform-regenerator": "^7.20.0",
"@definitelytyped/dtslint": "^0.0.127",
"@jest/create-cache-key-function": "^29.2.1",
"@react-native/metro-babel-transformer": "^0.73.11",
"@react-native/metro-config": "^0.73.0",
"@types/react": "^18.0.18",
"@typescript-eslint/parser": "^5.57.1",
Expand Down Expand Up @@ -85,7 +86,6 @@
"jscodeshift": "^0.14.0",
"metro-babel-register": "0.77.0",
"metro-memory-fs": "0.77.0",
"metro-react-native-babel-transformer": "0.77.0",
"mkdirp": "^0.5.1",
"mock-fs": "^5.1.4",
"prettier": "2.8.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Installation

```
yarn add --dev @react-native/js-polyfills metro-config metro-react-native-babel-transformer metro-runtime @react-native/metro-config
yarn add --dev @react-native/js-polyfills metro-config @react-native/metro-babel-transformer metro-runtime @react-native/metro-config
```

*Note: We're using `yarn` to install deps. Feel free to change commands to use `npm` 3+ and `npx` if you like*
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function getDefaultConfig(
'metro-runtime/src/modules/asyncRequire',
),
babelTransformerPath: require.resolve(
'metro-react-native-babel-transformer',
'@react-native/metro-babel-transformer',
),
getTransformOptions: async () => ({
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
},
"exports": "./index.js",
"dependencies": {
"@react-native/metro-babel-transformer": "^0.73.11",
"@react-native/js-polyfills": "^0.73.0",
"metro-config": "0.77.0",
"metro-react-native-babel-transformer": "0.77.0",
"metro-runtime": "0.77.0"
}
}
6 changes: 6 additions & 0 deletions packages/react-native-babel-preset/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/__mocks__/
**/__tests__/
/build/
/src.real/
/types/
yarn.lock
41 changes: 41 additions & 0 deletions packages/react-native-babel-preset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# @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 `@react-native/babel-preset` in your app:

with `npm`:

```sh
npm i @react-native/babel-preset --save-dev
```

or with `yarn`:

```sh
yarn add -D @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:@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.
63 changes: 63 additions & 0 deletions packages/react-native-babel-preset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@react-native/babel-preset",
"version": "0.73.15",
"description": "Babel preset for React Native applications",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "[email protected]:facebook/react-native.git"
},
"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"
}
}
17 changes: 17 additions & 0 deletions packages/react-native-babel-preset/src/configs/hmr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 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')],
};
};
93 changes: 93 additions & 0 deletions packages/react-native-babel-preset/src/configs/lazy-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* 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',
]);
Loading

0 comments on commit d380bb8

Please sign in to comment.