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';
|
|
|
|
|
|
|
|
const babelRegisterOnly = require('metro-babel-register');
|
|
|
|
const createCacheKeyFunction =
|
|
|
|
require('@jest/create-cache-key-function').default;
|
|
|
|
|
|
|
|
const {transformSync: babelTransformSync} = require('@babel/core');
|
|
|
|
const generate = require('@babel/generator').default;
|
|
|
|
|
|
|
|
const nodeFiles = new RegExp(
|
|
|
|
[
|
|
|
|
'/metro(?:-[^/]*)?/', // metro, metro-core, metro-source-map, metro-etc.
|
|
|
|
].join('|'),
|
|
|
|
);
|
|
|
|
|
2023-03-18 20:46:58 +00:00
|
|
|
// Use metro-babel-register to build the Babel configuration we need for Node
|
|
|
|
// files, but Jest takes care of hooking require so we don't actually register
|
|
|
|
// Babel here.
|
|
|
|
const nodeOptions = babelRegisterOnly.config([nodeFiles]);
|
2022-03-14 14:40:05 +00:00
|
|
|
|
|
|
|
const transformer = require('metro-react-native-babel-transformer');
|
|
|
|
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',
|
|
|
|
...nodeOptions,
|
|
|
|
ast: false,
|
2022-04-07 15:23:50 +00:00
|
|
|
});
|
2022-03-14 14:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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: false,
|
|
|
|
globalPrefix: '',
|
|
|
|
hot: false,
|
|
|
|
inlineRequires: true,
|
|
|
|
minify: false,
|
|
|
|
platform: '',
|
|
|
|
projectRoot: '',
|
|
|
|
publicPath: '/assets',
|
|
|
|
retainLines: true,
|
|
|
|
sourceType: 'unambiguous', // b7 required. detects module vs script mode
|
|
|
|
},
|
|
|
|
src,
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
getCacheKey: (createCacheKeyFunction([
|
|
|
|
__filename,
|
|
|
|
require.resolve('metro-react-native-babel-transformer'),
|
|
|
|
require.resolve('@babel/core/package.json'),
|
|
|
|
]) /*: any */),
|
|
|
|
};
|