2022-03-14 14:40:05 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2024-10-18 11:07:02 +00:00
|
|
|
// eslint-disable-next-line lint/sort-imports
|
2023-08-07 16:17:43 +00:00
|
|
|
const {
|
|
|
|
transformFromAstSync: babelTransformFromAstSync,
|
2024-10-18 11:07:02 +00:00
|
|
|
transformSync: babelTransformSync,
|
2023-08-07 16:17:43 +00:00
|
|
|
} = require('@babel/core');
|
2022-03-14 14:40:05 +00:00
|
|
|
const generate = require('@babel/generator').default;
|
2024-10-18 11:07:02 +00:00
|
|
|
const createCacheKeyFunction =
|
|
|
|
require('@jest/create-cache-key-function').default;
|
|
|
|
const metroBabelRegister = require('metro-babel-register');
|
|
|
|
const nullthrows = require('nullthrows');
|
2022-03-14 14:40:05 +00:00
|
|
|
|
2024-10-07 16:36:47 +00:00
|
|
|
if (process.env.FBSOURCE_ENV === '1') {
|
2024-09-27 17:36:42 +00:00
|
|
|
// If we're running in the Meta-internal monorepo, use the central Babel
|
|
|
|
// registration, which registers all of the relevant source directories
|
|
|
|
// including Metro's root.
|
|
|
|
//
|
|
|
|
// $FlowExpectedError[cannot-resolve-module] - Won't resolve in OSS
|
|
|
|
require('@fb-tools/babel-register');
|
2024-10-07 16:36:47 +00:00
|
|
|
} else {
|
2024-10-01 18:27:41 +00:00
|
|
|
// Register Babel to allow local packages to be loaded from source
|
|
|
|
require('../scripts/build/babel-register').registerForMonorepo();
|
2024-09-27 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-01 18:27:41 +00:00
|
|
|
const transformer = require('@react-native/metro-babel-transformer');
|
2024-09-27 17:36:42 +00:00
|
|
|
const metroTransformPlugins = require('metro-transform-plugins');
|
|
|
|
|
2023-04-11 17:08:53 +00:00
|
|
|
// Files matching this pattern will be transformed with the Node JS Babel
|
|
|
|
// transformer, rather than with the React Native Babel transformer. Scripts
|
|
|
|
// intended to run through Node JS should be included here.
|
|
|
|
const nodeFiles = /[\\/]metro(?:-[^/]*)[\\/]/;
|
|
|
|
|
|
|
|
// Get Babel config from metro-babel-register, without registering a require
|
|
|
|
// hook. This is used below to configure babelTransformSync under Jest.
|
|
|
|
const {only: _, ...nodeBabelOptions} = metroBabelRegister.config([]);
|
|
|
|
|
2024-01-27 14:49:15 +00:00
|
|
|
// Set BUILD_EXCLUDE_BABEL_REGISTER (see ../scripts/build/babel-register.js) to
|
|
|
|
// prevent inline Babel registration in code under test, normally required when
|
|
|
|
// running from source, but not in combination with the Jest transformer.
|
|
|
|
const babelPluginPreventBabelRegister = [
|
|
|
|
require.resolve('babel-plugin-transform-define'),
|
|
|
|
{
|
|
|
|
'process.env.BUILD_EXCLUDE_BABEL_REGISTER': true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-03-14 14:40:05 +00:00
|
|
|
module.exports = {
|
2022-04-07 15:23:50 +00:00
|
|
|
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ {
|
2022-03-14 14:40:05 +00:00
|
|
|
if (nodeFiles.test(file)) {
|
|
|
|
// node specific transforms only
|
|
|
|
return babelTransformSync(src, {
|
|
|
|
filename: file,
|
|
|
|
sourceType: 'script',
|
2023-04-11 17:08:53 +00:00
|
|
|
...nodeBabelOptions,
|
2024-01-27 14:49:15 +00:00
|
|
|
plugins: [
|
|
|
|
...(nodeBabelOptions.plugins ?? []),
|
|
|
|
babelPluginPreventBabelRegister,
|
|
|
|
],
|
2022-03-14 14:40:05 +00:00
|
|
|
ast: false,
|
2022-04-07 15:23:50 +00:00
|
|
|
});
|
2022-03-14 14:40:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 16:17:43 +00:00
|
|
|
let {ast} = transformer.transform({
|
2022-03-14 14:40:05 +00:00
|
|
|
filename: file,
|
|
|
|
options: {
|
|
|
|
ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044
|
|
|
|
dev: true,
|
|
|
|
enableBabelRuntime: false,
|
|
|
|
experimentalImportSupport: false,
|
|
|
|
globalPrefix: '',
|
2023-07-24 18:30:39 +00:00
|
|
|
hermesParser: true,
|
2022-03-14 14:40:05 +00:00
|
|
|
hot: false,
|
2023-08-07 16:17:43 +00:00
|
|
|
// $FlowFixMe[incompatible-call] TODO: Remove when `inlineRequires` has been removed from metro-babel-transformer in OSS
|
2022-03-14 14:40:05 +00:00
|
|
|
inlineRequires: true,
|
|
|
|
minify: false,
|
|
|
|
platform: '',
|
|
|
|
projectRoot: '',
|
|
|
|
publicPath: '/assets',
|
|
|
|
retainLines: true,
|
|
|
|
sourceType: 'unambiguous', // b7 required. detects module vs script mode
|
|
|
|
},
|
|
|
|
src,
|
|
|
|
});
|
|
|
|
|
2023-08-07 16:17:43 +00:00
|
|
|
const babelTransformResult = babelTransformFromAstSync(ast, src, {
|
|
|
|
ast: true,
|
|
|
|
retainLines: true,
|
|
|
|
plugins: [
|
2024-09-27 17:36:42 +00:00
|
|
|
metroTransformPlugins.inlineRequiresPlugin,
|
2024-01-27 14:49:15 +00:00
|
|
|
babelPluginPreventBabelRegister,
|
2023-08-07 16:17:43 +00:00
|
|
|
],
|
|
|
|
sourceType: 'module',
|
|
|
|
});
|
|
|
|
ast = nullthrows(babelTransformResult.ast);
|
|
|
|
|
2022-03-14 14:40:05 +00:00
|
|
|
return generate(
|
|
|
|
ast,
|
|
|
|
// $FlowFixMe[prop-missing] Error found when improving flow typing for libs
|
|
|
|
{
|
|
|
|
code: true,
|
|
|
|
comments: false,
|
|
|
|
compact: false,
|
|
|
|
filename: file,
|
|
|
|
retainLines: true,
|
|
|
|
sourceFileName: file,
|
|
|
|
sourceMaps: true,
|
|
|
|
},
|
|
|
|
src,
|
2022-04-07 15:23:50 +00:00
|
|
|
);
|
2022-03-14 14:40:05 +00:00
|
|
|
},
|
|
|
|
|
2023-06-15 15:23:06 +00:00
|
|
|
// $FlowFixMe[signature-verification-failure]
|
|
|
|
getCacheKey: createCacheKeyFunction([
|
2022-03-14 14:40:05 +00:00
|
|
|
__filename,
|
2023-07-27 18:48:16 +00:00
|
|
|
require.resolve('@react-native/metro-babel-transformer'),
|
2022-03-14 14:40:05 +00:00
|
|
|
require.resolve('@babel/core/package.json'),
|
2023-06-15 15:23:06 +00:00
|
|
|
]),
|
2022-03-14 14:40:05 +00:00
|
|
|
};
|