2022-03-14 17:40:05 +03: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';
|
|
|
|
|
2023-04-11 20:08:53 +03:00
|
|
|
const metroBabelRegister = require('metro-babel-register');
|
2022-03-14 17:40:05 +03:00
|
|
|
const createCacheKeyFunction =
|
|
|
|
require('@jest/create-cache-key-function').default;
|
|
|
|
|
|
|
|
const {transformSync: babelTransformSync} = require('@babel/core');
|
|
|
|
const generate = require('@babel/generator').default;
|
|
|
|
|
2023-04-11 20:08:53 +03: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([]);
|
|
|
|
|
|
|
|
const transformer = require('metro-react-native-babel-transformer');
|
2023-03-30 17:30:54 +03:00
|
|
|
|
2022-03-14 17:40:05 +03:00
|
|
|
module.exports = {
|
2022-04-07 18:23:50 +03:00
|
|
|
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ {
|
2022-03-14 17:40:05 +03:00
|
|
|
if (nodeFiles.test(file)) {
|
|
|
|
// node specific transforms only
|
|
|
|
return babelTransformSync(src, {
|
|
|
|
filename: file,
|
|
|
|
sourceType: 'script',
|
2023-04-11 20:08:53 +03:00
|
|
|
...nodeBabelOptions,
|
2022-03-14 17:40:05 +03:00
|
|
|
ast: false,
|
2022-04-07 18:23:50 +03:00
|
|
|
});
|
2022-03-14 17:40:05 +03: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: '',
|
2023-07-24 21:30:39 +03:00
|
|
|
hermesParser: true,
|
2022-03-14 17:40:05 +03:00
|
|
|
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 18:23:50 +03:00
|
|
|
);
|
2022-03-14 17:40:05 +03:00
|
|
|
},
|
|
|
|
|
2023-06-15 18:23:06 +03:00
|
|
|
// $FlowFixMe[signature-verification-failure]
|
|
|
|
getCacheKey: createCacheKeyFunction([
|
2022-03-14 17:40:05 +03:00
|
|
|
__filename,
|
|
|
|
require.resolve('metro-react-native-babel-transformer'),
|
|
|
|
require.resolve('@babel/core/package.json'),
|
2023-06-15 18:23:06 +03:00
|
|
|
]),
|
2022-03-14 17:40:05 +03:00
|
|
|
};
|