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
This commit is contained in:
Lauren Tan
2024-04-30 14:24:03 -04:00
parent 1181043d80
commit 86e0f87a5b
4 changed files with 52 additions and 2 deletions
@@ -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 ?? [],
});
@@ -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);
}
@@ -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<BabelCore.PluginItem> | 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,
},
};
}
@@ -98,3 +98,10 @@ export function hasNode<T>(
*/
return input.node != null;
}
export function hasOwnProperty<T>(
obj: T,
key: string | number | symbol
): key is keyof T {
return Object.prototype.hasOwnProperty.call(obj, key);
}