diff --git a/compiler/packages/babel-plugin-react-forget/src/Babel/BabelPlugin.ts b/compiler/packages/babel-plugin-react-forget/src/Babel/BabelPlugin.ts index be421366bd..be6df607a2 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Babel/BabelPlugin.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Babel/BabelPlugin.ts @@ -7,6 +7,10 @@ import type * as BabelCore from "@babel/core"; import { compileProgram, parsePluginOptions } from "../Entrypoint"; +import { + injectReanimatedFlag, + pipelineUsesReanimatedPlugin, +} from "../Entrypoint/Reanimated"; /* * The React Forget Babel Plugin @@ -25,8 +29,12 @@ export default function ReactForgetBabelPlugin( * want Forget to run true to source as possible. */ Program(prog, pass): void { + let opts = parsePluginOptions(pass.opts); + if (pipelineUsesReanimatedPlugin(pass.file.opts.plugins)) { + opts = injectReanimatedFlag(opts); + } compileProgram(prog, { - opts: parsePluginOptions(pass.opts), + opts, filename: pass.filename ?? null, comments: pass.file.ast.comments ?? [], }); diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts index bb3aef26df..ed93092ab9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts @@ -9,6 +9,7 @@ import * as t from "@babel/types"; import { z } from "zod"; import { CompilerErrorDetailOptions } from "../CompilerError"; import { ExternalFunction, PartialEnvironmentConfig } from "../HIR/Environment"; +import { hasOwnProperty } from "../Utils/utils"; const PanicThresholdOptionsSchema = z.enum([ /* @@ -204,5 +205,5 @@ export function parsePluginOptions(obj: unknown): PluginOptions { } function isCompilerFlag(s: string): s is keyof PluginOptions { - return Object.prototype.hasOwnProperty.call(defaultOptions, s); + return hasOwnProperty(defaultOptions, s); } diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Reanimated.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Reanimated.ts new file mode 100644 index 0000000000..4a3bb8bdba --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Reanimated.ts @@ -0,0 +1,34 @@ +import type * as BabelCore from "@babel/core"; +import { hasOwnProperty } from "../Utils/utils"; +import { PluginOptions } from "./Options"; + +export function pipelineUsesReanimatedPlugin( + plugins: Array | null | undefined +): boolean { + let hasReanimated = false; + if (Array.isArray(plugins)) { + for (const plugin of plugins) { + if (hasOwnProperty(plugin, "key")) { + const key = (plugin as any).key; // already checked + if ( + typeof key === "string" && + key.indexOf("react-native-reanimated") !== -1 + ) { + hasReanimated = true; + break; + } + } + } + } + return hasReanimated; +} + +export function injectReanimatedFlag(options: PluginOptions): PluginOptions { + return { + ...options, + environment: { + ...options.environment, + enableCustomTypeDefinitionForReAnimated: true, + }, + }; +} diff --git a/compiler/packages/babel-plugin-react-forget/src/Utils/utils.ts b/compiler/packages/babel-plugin-react-forget/src/Utils/utils.ts index 43a5fbed60..c02e813255 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Utils/utils.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Utils/utils.ts @@ -98,3 +98,10 @@ export function hasNode( */ return input.node != null; } + +export function hasOwnProperty( + obj: T, + key: string | number | symbol +): key is keyof T { + return Object.prototype.hasOwnProperty.call(obj, key); +}