diff --git a/packages/react-native-babel-preset/.npmignore b/packages/react-native-babel-preset/.npmignore new file mode 100644 index 00000000000000..0ec3b99a14c7b7 --- /dev/null +++ b/packages/react-native-babel-preset/.npmignore @@ -0,0 +1,6 @@ +**/__mocks__/ +**/__tests__/ +/build/ +/src.real/ +/types/ +yarn.lock diff --git a/packages/react-native-babel-preset/README.md b/packages/react-native-babel-preset/README.md new file mode 100644 index 00000000000000..9f39e235c2a61a --- /dev/null +++ b/packages/react-native-babel-preset/README.md @@ -0,0 +1,41 @@ +# 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/react-native-babel-preset/package.json b/packages/react-native-babel-preset/package.json new file mode 100644 index 00000000000000..fbd204ac0212c8 --- /dev/null +++ b/packages/react-native-babel-preset/package.json @@ -0,0 +1,67 @@ +{ + "name": "metro-react-native-babel-preset", + "version": "0.76.7", + "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/react-native-babel-preset/src/configs/hmr.js b/packages/react-native-babel-preset/src/configs/hmr.js new file mode 100644 index 00000000000000..46cbcbd485e61a --- /dev/null +++ b/packages/react-native-babel-preset/src/configs/hmr.js @@ -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')], + }; +}; diff --git a/packages/react-native-babel-preset/src/configs/lazy-imports.js b/packages/react-native-babel-preset/src/configs/lazy-imports.js new file mode 100644 index 00000000000000..1c001f67a91f15 --- /dev/null +++ b/packages/react-native-babel-preset/src/configs/lazy-imports.js @@ -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', +]); diff --git a/packages/react-native-babel-preset/src/configs/main.js b/packages/react-native-babel-preset/src/configs/main.js new file mode 100644 index 00000000000000..675d51de5f9fbb --- /dev/null +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -0,0 +1,215 @@ +/** + * 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/react-native-babel-preset/src/index.js b/packages/react-native-babel-preset/src/index.js new file mode 100644 index 00000000000000..011dfe06405212 --- /dev/null +++ b/packages/react-native-babel-preset/src/index.js @@ -0,0 +1,20 @@ +/** + * 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/react-native-babel-preset/src/passthrough-syntax-plugins.js b/packages/react-native-babel-preset/src/passthrough-syntax-plugins.js new file mode 100644 index 00000000000000..f66f225c64076b --- /dev/null +++ b/packages/react-native-babel-preset/src/passthrough-syntax-plugins.js @@ -0,0 +1,23 @@ +/** + * 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/react-native-babel-transformer/.npmignore b/packages/react-native-babel-transformer/.npmignore new file mode 100644 index 00000000000000..0ec3b99a14c7b7 --- /dev/null +++ b/packages/react-native-babel-transformer/.npmignore @@ -0,0 +1,6 @@ +**/__mocks__/ +**/__tests__/ +/build/ +/src.real/ +/types/ +yarn.lock diff --git a/packages/react-native-babel-transformer/package.json b/packages/react-native-babel-transformer/package.json new file mode 100644 index 00000000000000..877ef5b6868e98 --- /dev/null +++ b/packages/react-native-babel-transformer/package.json @@ -0,0 +1,33 @@ +{ + "name": "metro-react-native-babel-transformer", + "version": "0.76.7", + "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.14.0", + "metro-react-native-babel-preset": "0.76.7", + "nullthrows": "^1.1.1" + }, + "peerDependencies": { + "@babel/core": "*" + }, + "engines": { + "node": ">=18" + } +} diff --git a/packages/react-native-babel-transformer/src/__tests__/transform-test.js b/packages/react-native-babel-transformer/src/__tests__/transform-test.js new file mode 100644 index 00000000000000..3919cbfa296975 --- /dev/null +++ b/packages/react-native-babel-transformer/src/__tests__/transform-test.js @@ -0,0 +1,53 @@ +/** + * 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/react-native-babel-transformer/src/index.js b/packages/react-native-babel-transformer/src/index.js new file mode 100644 index 00000000000000..80384e937a6db8 --- /dev/null +++ b/packages/react-native-babel-transformer/src/index.js @@ -0,0 +1,255 @@ +/** + * 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/yarn.lock b/yarn.lock index 5d963caa84a698..5e00fb52219b17 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6892,51 +6892,6 @@ metro-react-native-babel-preset@0.76.2: babel-plugin-transform-flow-enums "^0.0.2" react-refresh "^0.4.0" -metro-react-native-babel-preset@0.76.7: - version "0.76.7" - resolved "https://registry.yarnpkg.com/metro-react-native-babel-preset/-/metro-react-native-babel-preset-0.76.7.tgz#dfe15c040d0918147a8b0e9f530d558287acbb54" - integrity sha512-R25wq+VOSorAK3hc07NW0SmN8z9S/IR0Us0oGAsBcMZnsgkbOxu77Mduqf+f4is/wnWHc5+9bfiqdLnaMngiVw== - 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" - metro-react-native-babel-transformer@0.76.2: version "0.76.2" resolved "https://registry.yarnpkg.com/metro-react-native-babel-transformer/-/metro-react-native-babel-transformer-0.76.2.tgz#84865806bcbd9376db8190c33f2b7cd178fa342b" @@ -6950,17 +6905,6 @@ metro-react-native-babel-transformer@0.76.2: metro-source-map "0.76.2" nullthrows "^1.1.1" -metro-react-native-babel-transformer@0.76.7: - version "0.76.7" - resolved "https://registry.yarnpkg.com/metro-react-native-babel-transformer/-/metro-react-native-babel-transformer-0.76.7.tgz#ccc7c25b49ee8a1860aafdbf48bfa5441d206f8f" - integrity sha512-W6lW3J7y/05ph3c2p3KKJNhH0IdyxdOCbQ5it7aM2MAl0SM4wgKjaV6EYv9b3rHklpV6K3qMH37UKVcjMooWiA== - dependencies: - "@babel/core" "^7.20.0" - babel-preset-fbjs "^3.4.0" - hermes-parser "0.12.0" - metro-react-native-babel-preset "0.76.7" - nullthrows "^1.1.1" - metro-resolver@0.76.7: version "0.76.7" resolved "https://registry.yarnpkg.com/metro-resolver/-/metro-resolver-0.76.7.tgz#f00ebead64e451c060f30926ecbf4f797588df52"