Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add name to the Reanimated Plugin #6705

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-native-reanimated/plugin/index.js

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions packages/react-native-reanimated/plugin/src/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ReanimatedPluginPass } from './types';

const notCapturedIdentifiers = [
// Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

Expand Down Expand Up @@ -152,6 +154,12 @@ const notCapturedIdentifiers_DEPRECATED = [
'_getAnimationTimestamp',
];

export function initializeState(state: ReanimatedPluginPass) {
state.workletNumber = 1;
initializeGlobals();
addCustomGlobals(state);
}

export const defaultGlobals = new Set(
notCapturedIdentifiers.concat(notCapturedIdentifiers_DEPRECATED)
);
Expand All @@ -161,3 +169,23 @@ export let globals: Set<string>;
export function initializeGlobals() {
globals = new Set(defaultGlobals);
}

/**
* This function allows to add custom globals such as host-functions. Those
* globals have to be passed as an argument for the plugin in babel.config.js.
*
* For example:
*
* ```js
* plugins: [
* ['react-native-reanimated/plugin', { globals: ['myHostFunction'] }],
* ];
* ```
*/
export function addCustomGlobals(state: ReanimatedPluginPass) {
if (state.opts && Array.isArray(state.opts.globals)) {
state.opts.globals.forEach((name: string) => {
globals.add(name);
});
}
}
tjzel marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 6 additions & 10 deletions packages/react-native-reanimated/plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import {
processCalleesAutoworkletizableCallbacks,
processIfAutoworkletizableCallback,
} from './autoworkletization';
import { processIfWorkletClass } from './class';
import { processIfWorkletContextObject } from './contextObject';
import { processIfWorkletFile } from './file';
import { initializeGlobals } from './globals';
import { initializeState } from './globals';
import { processInlineStylesWarning } from './inlineStylesWarning';
import type { ReanimatedPluginPass } from './types';
import { WorkletizableFunction } from './types';
import { addCustomGlobals } from './utils';
import { substituteWebCallExpression } from './webOptimization';
import { processIfWithWorkletDirective } from './workletSubstitution';
import { processIfWorkletClass } from './class';

module.exports = function (): PluginItem {
function runWithTaggedExceptions(fun: () => void) {
Expand All @@ -31,12 +30,11 @@ module.exports = function (): PluginItem {
}

return {
pre(state: ReanimatedPluginPass) {
name: 'reanimated',

pre(this: ReanimatedPluginPass) {
runWithTaggedExceptions(() => {
// Initialize worklet number.
state.workletNumber = 1;
initializeGlobals();
addCustomGlobals.call(this);
initializeState(this);
});
},
visitor: {
Expand Down Expand Up @@ -78,8 +76,6 @@ module.exports = function (): PluginItem {
Program: {
enter(path: NodePath<Program>, state: ReanimatedPluginPass) {
runWithTaggedExceptions(() => {
// Reset worklet number.
state.workletNumber = 1;
tjzel marked this conversation as resolved.
Show resolved Hide resolved
processIfWorkletFile(path, state);
});
},
Expand Down
3 changes: 0 additions & 3 deletions packages/react-native-reanimated/plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ export interface ReanimatedPluginPass {
opts: ReanimatedPluginOptions;
cwd: string;
filename: string | undefined;
get(key: unknown): unknown;
set(key: unknown, value: unknown): void;
workletNumber: number;
[key: string]: unknown;
}

export type WorkletizableFunction =
Expand Down
22 changes: 0 additions & 22 deletions packages/react-native-reanimated/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
variableDeclaration,
variableDeclarator,
} from '@babel/types';
import { globals } from './globals';
import type { ReanimatedPluginPass } from './types';

export function isRelease() {
const pattern = /(prod|release|stag[ei])/i;
Expand All @@ -18,26 +16,6 @@ export function isRelease() {
);
}

/**
* This function allows to add custom globals such as host-functions. Those
* globals have to be passed as an argument for the plugin in babel.config.js.
*
* For example:
*
* ```js
* plugins: [
* ['react-native-reanimated/plugin', { globals: ['myHostFunction'] }],
* ];
* ```
*/
export function addCustomGlobals(this: ReanimatedPluginPass) {
if (this.opts && Array.isArray(this.opts.globals)) {
this.opts.globals.forEach((name: string) => {
globals.add(name);
});
}
}

/**
* This function replaces the node with a factory call while making sure that
* it's a legal operation. If the node cannot be simply replaced with a factory
Expand Down