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: 46f7aec0bc3035a92369f65b0842bafab0089a9e Pull Request resolved: https://github.com/facebook/react-forget/pull/2953
Lauren Tan committed
May 11, 2024 at 00:19 UTC
9b0044e21387f10b005b58fb50c2ea4fcf2f228f
1 file changed
+23
-4
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Reanimated.ts
+23
-4
@@ -2,10 +2,30 @@ import type * as BabelCore from "@babel/core";
2
import { hasOwnProperty } from "../Utils/utils";
3
import { PluginOptions } from "./Options";
4
5
+function hasModule(name: string): boolean {
6
+ try {
7
+ return !!require.resolve(name);
8
+ } catch (error: any) {
9
+ if (
10
+ error.code === "MODULE_NOT_FOUND" &&
11
+ error.message.indexOf(name) !== -1
12
+ ) {
13
+ return false;
14
+ }
15
+ throw error;
16
+ }
17
+}
18
+
19
+/**
20
+ * Tries to detect if reanimated is installed by first looking for the presence of the babel plugin.
21
+ * However, babel-preset-expo includes it by default so it is occasionally ommitted. If so, we do
22
+ * a check to see if `react-native-animated` is requireable.
23
+ *
24
+ * See https://github.com/expo/expo/blob/e4b8d86442482c7316365a6b7ec1141eec73409d/packages/babel-preset-expo/src/index.ts#L300-L301
25
+ */
26
export function pipelineUsesReanimatedPlugin(
27
plugins: Array<BabelCore.PluginItem> | null | undefined
28
): boolean {
8
- let hasReanimated = false;
29
if (Array.isArray(plugins)) {
30
for (const plugin of plugins) {
31
if (hasOwnProperty(plugin, "key")) {
@@ -14,13 +34,12 @@ export function pipelineUsesReanimatedPlugin(
34
typeof key === "string" &&
35
key.indexOf("react-native-reanimated") !== -1
36
) {
17
- hasReanimated = true;
18
- break;
37
+ return true;
38
}
39
}
40
}
41
}
23
- return hasReanimated;
42
+ return hasModule("react-native-reanimated");
43
}
44
45
export function injectReanimatedFlag(options: PluginOptions): PluginOptions {