@samitouri / QOS-React / commits / 86e0f87a5b

Dynamically detect reanimated plugin

Enables the Reanimated flag automatically if we find reanimated in the user's list of plugins ghstack-source-id: 20e83374612362a30d6c8cc7a903d9320e8cc23a Pull Request resolved: https://github.com/facebook/react-forget/pull/2915

Lauren Tan committed Apr 30, 2024 at 14:24 UTC 86e0f87a5bf40ef08c8072a2a5800d755f68e194
4 files changed +52 -2
compiler/packages/babel-plugin-react-forget/src/Babel/BabelPlugin.ts
+9 -1
@@ -7,6 +7,10 @@
7
8 import type * as BabelCore from "@babel/core";
9 import { compileProgram, parsePluginOptions } from "../Entrypoint";
10 +import {
11 + injectReanimatedFlag,
12 + pipelineUsesReanimatedPlugin,
13 +} from "../Entrypoint/Reanimated";
14
15 /*
16 * The React Forget Babel Plugin
@@ -25,8 +29,12 @@ export default function ReactForgetBabelPlugin(
29 * want Forget to run true to source as possible.
30 */
31 Program(prog, pass): void {
32 + let opts = parsePluginOptions(pass.opts);
33 + if (pipelineUsesReanimatedPlugin(pass.file.opts.plugins)) {
34 + opts = injectReanimatedFlag(opts);
35 + }
36 compileProgram(prog, {
29 - opts: parsePluginOptions(pass.opts),
37 + opts,
38 filename: pass.filename ?? null,
39 comments: pass.file.ast.comments ?? [],
40 });
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts
+2 -1
@@ -9,6 +9,7 @@ import * as t from "@babel/types";
9 import { z } from "zod";
10 import { CompilerErrorDetailOptions } from "../CompilerError";
11 import { ExternalFunction, PartialEnvironmentConfig } from "../HIR/Environment";
12 +import { hasOwnProperty } from "../Utils/utils";
13
14 const PanicThresholdOptionsSchema = z.enum([
15 /*
@@ -204,5 +205,5 @@ export function parsePluginOptions(obj: unknown): PluginOptions {
205 }
206
207 function isCompilerFlag(s: string): s is keyof PluginOptions {
207 - return Object.prototype.hasOwnProperty.call(defaultOptions, s);
208 + return hasOwnProperty(defaultOptions, s);
209 }
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Reanimated.ts new
+34
@@ -0,0 +1,34 @@
1 +import type * as BabelCore from "@babel/core";
2 +import { hasOwnProperty } from "../Utils/utils";
3 +import { PluginOptions } from "./Options";
4 +
5 +export function pipelineUsesReanimatedPlugin(
6 + plugins: Array<BabelCore.PluginItem> | null | undefined
7 +): boolean {
8 + let hasReanimated = false;
9 + if (Array.isArray(plugins)) {
10 + for (const plugin of plugins) {
11 + if (hasOwnProperty(plugin, "key")) {
12 + const key = (plugin as any).key; // already checked
13 + if (
14 + typeof key === "string" &&
15 + key.indexOf("react-native-reanimated") !== -1
16 + ) {
17 + hasReanimated = true;
18 + break;
19 + }
20 + }
21 + }
22 + }
23 + return hasReanimated;
24 +}
25 +
26 +export function injectReanimatedFlag(options: PluginOptions): PluginOptions {
27 + return {
28 + ...options,
29 + environment: {
30 + ...options.environment,
31 + enableCustomTypeDefinitionForReAnimated: true,
32 + },
33 + };
34 +}
compiler/packages/babel-plugin-react-forget/src/Utils/utils.ts
+7
@@ -98,3 +98,10 @@ export function hasNode<T>(
98 */
99 return input.node != null;
100 }
101 +
102 +export function hasOwnProperty<T>(
103 + obj: T,
104 + key: string | number | symbol
105 +): key is keyof T {
106 + return Object.prototype.hasOwnProperty.call(obj, key);
107 +}