react-native/jest/preprocessor.js

102 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* 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('|'),
);
Bump RN CLI, add @react-native/metro-config to template (#36623) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36623 Changelog: [General][Changed] - The default `metro.config.js` in apps now extends `react-native/metro-config`, and should be updated in existing apps. ~~`react-native/rn-get-polyfills.js` is removed and should be updated to `react-native/js-polyfills` in existing apps (this is part of the new default config).~~ #publish-packages-to-npm ## Context ### React Native Metro config → React Native repo (https://github.com/facebook/react-native/pull/36502) We (the React Native team) are aiming to relocate the default Metro config for React Native out of `react-native-community/cli-plugin-metro` and **into the React Native repo + app template** as a new `react-native/metro-config` package. This is the first (and minimum viable) phase we can ship to separate the release process of Metro from RN CLI in order to reduce coupling and iterate faster for our users. **See full motivation, design, and test plan (which previewed the CLI bump) here: https://github.com/facebook/react-native/pull/36502** ## Changes NOTE: This PR is pending the inclusion of a bump to `react-native-community/cli`, and will be sequenced after https://github.com/react-native-community/cli/pull/1875 is merged. - Upgrade `react-native-community/cli` to `11.0.0`, upgrade all `metro` packages to `0.76.0` (version distributed in this CLI release). - Update the `metro.config.js` file in `packages/react-native/template/`. - Now merges defaults from `react-native/metro-config`, and can be used with CLI >= 11.0.0. - Update the `metro.config.js` files for `packages/react-native/` and `packages/rn-tester/` (these are integration test locations). - Now merges defaults from `react-native/metro-config`, and can be used with CLI >= 11.0.0. Changes to `react-native/metro-config` — `0.72.1` (prepared but not depended on yet): - Export `mergeConfig` util (removing direct `metro-config` dependency in consuming projects). - Explicitly depend on `metro-react-native-babel-transformer` and `metro-runtime` (transitively included today). Reviewed By: cortinico, blakef Differential Revision: D44099691 fbshipit-source-id: 405635dd69fd50a1e9548279eaeda3c932b5b167
2023-03-30 14:30:54 +00:00
// Get Babel config from metro-babel-register, without registering against
// `require`. This is used below to configure babelTransformSync under Jest.
const nodeOptions = babelRegisterOnly.config([nodeFiles]);
Bump RN CLI, add @react-native/metro-config to template (#36623) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36623 Changelog: [General][Changed] - The default `metro.config.js` in apps now extends `react-native/metro-config`, and should be updated in existing apps. ~~`react-native/rn-get-polyfills.js` is removed and should be updated to `react-native/js-polyfills` in existing apps (this is part of the new default config).~~ #publish-packages-to-npm ## Context ### React Native Metro config → React Native repo (https://github.com/facebook/react-native/pull/36502) We (the React Native team) are aiming to relocate the default Metro config for React Native out of `react-native-community/cli-plugin-metro` and **into the React Native repo + app template** as a new `react-native/metro-config` package. This is the first (and minimum viable) phase we can ship to separate the release process of Metro from RN CLI in order to reduce coupling and iterate faster for our users. **See full motivation, design, and test plan (which previewed the CLI bump) here: https://github.com/facebook/react-native/pull/36502** ## Changes NOTE: This PR is pending the inclusion of a bump to `react-native-community/cli`, and will be sequenced after https://github.com/react-native-community/cli/pull/1875 is merged. - Upgrade `react-native-community/cli` to `11.0.0`, upgrade all `metro` packages to `0.76.0` (version distributed in this CLI release). - Update the `metro.config.js` file in `packages/react-native/template/`. - Now merges defaults from `react-native/metro-config`, and can be used with CLI >= 11.0.0. - Update the `metro.config.js` files for `packages/react-native/` and `packages/rn-tester/` (these are integration test locations). - Now merges defaults from `react-native/metro-config`, and can be used with CLI >= 11.0.0. Changes to `react-native/metro-config` — `0.72.1` (prepared but not depended on yet): - Export `mergeConfig` util (removing direct `metro-config` dependency in consuming projects). - Explicitly depend on `metro-react-native-babel-transformer` and `metro-runtime` (transitively included today). Reviewed By: cortinico, blakef Differential Revision: D44099691 fbshipit-source-id: 405635dd69fd50a1e9548279eaeda3c932b5b167
2023-03-30 14:30:54 +00:00
let transformer;
try {
transformer = require('metro-react-native-babel-transformer');
} catch (e) {
if (e.name !== 'SyntaxError') {
throw e;
}
// [fbsource only] When Metro dependency versions match the latest release,
// they are loaded from source (facebook/metro lives inside Meta's monorepo).
// We need babel-register to use the transformer in this configuration file.
babelRegisterOnly([]);
transformer = require('metro-react-native-babel-transformer');
}
module.exports = {
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ {
if (nodeFiles.test(file)) {
// node specific transforms only
return babelTransformSync(src, {
filename: file,
sourceType: 'script',
...nodeOptions,
ast: false,
});
}
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,
);
},
getCacheKey: (createCacheKeyFunction([
__filename,
require.resolve('metro-react-native-babel-transformer'),
require.resolve('@babel/core/package.json'),
]) /*: any */),
};