Better detection for reanimated

We found this issue through enabling the compiler on the React Conf app.

`babel-preset-expo` automatically adds the `react-native-animated`
plugin to apps that use the preset. This means that Expo apps sometimes
omit the react-native-animated plugin from their config, which was
failing our existing check. This PR copies the same detection that Expo
does for adding reanimated as a fallback

ghstack-source-id: 46f7aec0bc
Pull Request resolved: https://github.com/facebook/react-forget/pull/2953
This commit is contained in:
Lauren Tan
2024-05-11 00:19:59 -04:00
parent c58142ba35
commit 9b0044e213
@@ -2,10 +2,30 @@ import type * as BabelCore from "@babel/core";
import { hasOwnProperty } from "../Utils/utils";
import { PluginOptions } from "./Options";
function hasModule(name: string): boolean {
try {
return !!require.resolve(name);
} catch (error: any) {
if (
error.code === "MODULE_NOT_FOUND" &&
error.message.indexOf(name) !== -1
) {
return false;
}
throw error;
}
}
/**
* Tries to detect if reanimated is installed by first looking for the presence of the babel plugin.
* However, babel-preset-expo includes it by default so it is occasionally ommitted. If so, we do
* a check to see if `react-native-animated` is requireable.
*
* See https://github.com/expo/expo/blob/e4b8d86442482c7316365a6b7ec1141eec73409d/packages/babel-preset-expo/src/index.ts#L300-L301
*/
export function pipelineUsesReanimatedPlugin(
plugins: Array<BabelCore.PluginItem> | null | undefined
): boolean {
let hasReanimated = false;
if (Array.isArray(plugins)) {
for (const plugin of plugins) {
if (hasOwnProperty(plugin, "key")) {
@@ -14,13 +34,12 @@ export function pipelineUsesReanimatedPlugin(
typeof key === "string" &&
key.indexOf("react-native-reanimated") !== -1
) {
hasReanimated = true;
break;
return true;
}
}
}
}
return hasReanimated;
return hasModule("react-native-reanimated");
}
export function injectReanimatedFlag(options: PluginOptions): PluginOptions {