| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type * as BabelCore from '@babel/core'; |
| 9 | import {hasOwnProperty} from '../Utils/utils'; |
| 10 | import {ParsedPluginOptions} from './Options'; |
| 11 | |
| 12 | function hasModule(name: string): boolean { |
| 13 | if (typeof require === 'undefined') { |
| 14 | return false; |
| 15 | } |
| 16 | try { |
| 17 | return !!require.resolve(name); |
| 18 | } catch (error: any) { |
| 19 | if ( |
| 20 | error.code === 'MODULE_NOT_FOUND' && |
| 21 | error.message.indexOf(name) !== -1 |
| 22 | ) { |
| 23 | return false; |
| 24 | } |
| 25 | throw error; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Tries to detect if reanimated is installed by first looking for the presence of the babel plugin. |
| 31 | * However, babel-preset-expo includes it by default so it is occasionally ommitted. If so, we do |
| 32 | * a check to see if `react-native-animated` is requireable. |
| 33 | * |
| 34 | * See https://github.com/expo/expo/blob/e4b8d86442482c7316365a6b7ec1141eec73409d/packages/babel-preset-expo/src/index.ts#L300-L301 |
| 35 | */ |
| 36 | export function pipelineUsesReanimatedPlugin( |
| 37 | plugins: Array<BabelCore.PluginItem> | null | undefined, |
| 38 | ): boolean { |
| 39 | if (Array.isArray(plugins)) { |
| 40 | for (const plugin of plugins) { |
| 41 | if (hasOwnProperty(plugin, 'key')) { |
| 42 | const key = (plugin as any).key; // already checked |
| 43 | if ( |
| 44 | typeof key === 'string' && |
| 45 | key.indexOf('react-native-reanimated') !== -1 |
| 46 | ) { |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | return hasModule('react-native-reanimated'); |
| 53 | } |
| 54 | |
| 55 | export function injectReanimatedFlag( |
| 56 | options: ParsedPluginOptions, |
| 57 | ): ParsedPluginOptions { |
| 58 | return { |
| 59 | ...options, |
| 60 | environment: { |
| 61 | ...options.environment, |
| 62 | enableCustomTypeDefinitionForReanimated: true, |
| 63 | }, |
| 64 | }; |
| 65 | } |