Files
react-native/scripts/babel-register.js
T
Alex Hunt 3cf400a51b Convert build scripts to regular Flow syntax (#49103)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49103

Changelog: [Internal]

Reviewed By: j-piasecki

Differential Revision: D68960540

fbshipit-source-id: 0ac01529eaea97db98b85b6021532092997d633b
2025-02-03 03:49:23 -08:00

76 lines
1.7 KiB
JavaScript

/**
* 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
* @oncall react_native
*/
const {
PACKAGES_DIR,
RN_INTEGRATION_TESTS_RUNNER_DIR,
SCRIPTS_DIR,
} = require('./consts');
let isRegisteredForMonorepo = false;
let isRegisteredForScriptsDir = false;
/**
* Calling this function enables all Node.js packages to run from source when
* developing in the React Native repo.
*
* A call should located in each entry point file in a package (i.e. for all
* paths in "exports"), inside a special `if` condition that will be compiled
* away on build.
*
* ```js
* // Place in a package entry point
* if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) {
* require('../../../scripts/babel-register').registerForMonorepo();
* }
* ```
*/
function registerForMonorepo() {
if (isRegisteredForMonorepo) {
return;
}
if (process.env.FBSOURCE_ENV === '1') {
// $FlowExpectedError[cannot-resolve-module] - Won't resolve in OSS
require('@fb-tools/babel-register');
} else {
require('metro-babel-register')([
PACKAGES_DIR,
RN_INTEGRATION_TESTS_RUNNER_DIR,
]);
}
isRegisteredForMonorepo = true;
}
/**
* Calling this function enables entry points under scripts/ to run from source.
*
* ```js
* // Place in a script entry point
* require('../babel-register').registerForScript();
* ```
*/
function registerForScript() {
if (isRegisteredForScriptsDir) {
return;
}
require('metro-babel-register')([SCRIPTS_DIR]);
isRegisteredForScriptsDir = true;
}
module.exports = {
registerForMonorepo,
registerForScript,
};