Skip to content

Commit

Permalink
feat: Add name to the Reanimated Plugin (#6705)
Browse files Browse the repository at this point in the history
## Summary

Babel plugin can have names. Plugin will now be named `reanimated`
instead of `node_modules/react-native-reanimated/plugin/index.js`.

Cleaning some files as a bonus.

## Test plan

🚀
  • Loading branch information
tjzel committed Nov 20, 2024
1 parent f941612 commit 1c029d7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
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);
});
}
}
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;
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

0 comments on commit 1c029d7

Please sign in to comment.