-
Notifications
You must be signed in to change notification settings - Fork 57
[RUM-9982] Set up React Native babel plugin project #887
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
Merged
cdn34dd
merged 1 commit into
carlosnogueira/RUM-9145/refactor-rum-action-tracking
from
carlosnogueira/RUM-9982/setup-base-package-for-babel-plugin
May 28, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| { | ||
| "name": "@datadog/mobile-react-native-babel-plugin", | ||
| "version": "2.8.0", | ||
| "description": "A Babel plugin that enhances Datadog's React Native SDK by automatically enriching React components with contextual metadata.", | ||
| "keywords": [ | ||
| "babel", | ||
| "babel-plugin", | ||
| "plugin", | ||
| "datadog", | ||
| "react-native" | ||
| ], | ||
| "author": "Datadog (https://github.com/DataDog)", | ||
| "homepage": "https://github.com/DataDog/dd-sdk-reactnative#readme", | ||
| "repository": "https://github.com/DataDog/dd-sdk-reactnative", | ||
| "bugs": { | ||
| "url": "https://github.com/DataDog/dd-sdk-reactnative/issues" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "main": "lib/commonjs/index", | ||
| "files": [ | ||
| "src/**", | ||
| "lib" | ||
| ], | ||
| "types": "lib/typescript/index.d.ts", | ||
| "react-native": "src/index", | ||
| "source": "src", | ||
| "module": "lib/module/index", | ||
| "publishConfig": { | ||
| "access": "private" | ||
| }, | ||
| "scripts": { | ||
| "test": "jest", | ||
| "lint": "eslint .", | ||
| "prepare": "rm -rf lib && yarn bob build" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": ">=16.13.1", | ||
| "react-native": ">=0.63.4 <1.0" | ||
| }, | ||
| "dependencies": { | ||
| "@babel/core": "^7.27.1", | ||
| "@babel/helper-plugin-utils": "^7.27.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.27.2", | ||
| "@babel/preset-react": "^7.27.1", | ||
| "@babel/preset-typescript": "^7.27.1", | ||
| "@babel/types": "^7.27.1", | ||
| "react-native-builder-bob": "0.26.0", | ||
| "tsc-alias": "^1.8.16", | ||
| "typescript": "5.0.4" | ||
| }, | ||
| "react-native-builder-bob": { | ||
| "source": "src", | ||
| "output": "lib", | ||
| "targets": [ | ||
| "commonjs", | ||
| "module", | ||
| [ | ||
| "typescript", | ||
| { | ||
| "tsc": "./../../node_modules/.bin/tsc" | ||
| } | ||
| ] | ||
| ] | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
packages/react-native-babel-plugin/src/actions/global/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| import type * as Babel from '@babel/core'; | ||
|
|
||
| import { PluginConstants } from '../../constants'; | ||
| import type { BabelTypes } from '../../types'; | ||
| import { | ||
| getAssignmentNode, | ||
| insertAtProgramTop, | ||
| PluginState | ||
| } from '../../utils'; | ||
|
|
||
| export function insertSetupFlag( | ||
| path: Babel.NodePath<Babel.types.Program>, | ||
| t: BabelTypes | ||
| ) { | ||
| const pluginState = PluginState.getInstance(); | ||
| // Only set the flag on the entry file of the project | ||
| if (pluginState.isInitialized) { | ||
| return; | ||
| } | ||
|
|
||
| pluginState.isInitialized = true; | ||
|
|
||
| const flagNode = getAssignmentNode( | ||
| t, | ||
| 'globalThis', | ||
| PluginConstants.PLUGIN_ENABLED, | ||
| t.booleanLiteral(true) | ||
| ); | ||
|
|
||
| insertAtProgramTop(path, flagNode); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| export const PluginConstants = { | ||
| PLUGIN_ENABLED: '__DD_RN_BABEL_PLUGIN_ENABLED__' | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| export * from './global'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| import type * as Babel from '@babel/core'; | ||
| import { declare } from '@babel/helper-plugin-utils'; | ||
|
|
||
| import { insertSetupFlag } from './actions/global'; | ||
| import type { PluginOptions } from './types'; | ||
| import { getFileInfo } from './utils/index'; | ||
|
|
||
| export default declare( | ||
| ( | ||
| api: typeof Babel & Babel.ConfigAPI, | ||
| _options: PluginOptions, | ||
| _dirname: string | ||
| ): Babel.PluginObj<Babel.PluginPass> => { | ||
| api.assertVersion(7); | ||
|
|
||
| return { | ||
| visitor: { | ||
| Program(path, _state) { | ||
| const { path: _p, name: _name } = getFileInfo(this); | ||
| insertSetupFlag(path, api.types); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| export type PluginOptions = { | ||
| /* empty */ | ||
| }; |
21 changes: 21 additions & 0 deletions
21
packages/react-native-babel-plugin/src/types/helper-plugin-utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| declare module '@babel/helper-plugin-utils' { | ||
| import type * as Babel from '@babel/core'; | ||
|
|
||
| export function declare<T = unknown>( | ||
| builder: ( | ||
| api: typeof Babel & Babel.ConfigAPI, | ||
| options: T, | ||
| dirname: string | ||
| ) => Babel.PluginObj<Babel.PluginPass> | ||
| ): ( | ||
| api: Babel.ConfigAPI, | ||
| options: T, | ||
| dirname: string | ||
| ) => Babel.PluginObj<Babel.PluginPass>; | ||
| } | ||
|
cdn34dd marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| export * from './general'; | ||
| export * from './nodes'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| import type * as Babel from '@babel/core'; | ||
|
|
||
| export type BabelTypes = typeof Babel.types; | ||
|
|
||
| export type AssignmentNode = | ||
| | Babel.types.Identifier | ||
| | Babel.types.StringLiteral | ||
| | Babel.types.NumericLiteral | ||
| | Babel.types.NullLiteral | ||
| | Babel.types.BooleanLiteral | ||
| | Babel.types.RegExpLiteral | ||
| | Babel.types.ObjectExpression | ||
| | Babel.types.ArrayExpression; |
31 changes: 31 additions & 0 deletions
31
packages/react-native-babel-plugin/src/utils/PluginState.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| const PluginStateErrors = { | ||
| ALREADY_INITIALIZED: | ||
| 'Plugin State already initialized, please use `getInstance`.' | ||
| } as const; | ||
|
|
||
| export class PluginState { | ||
| static instance: PluginState | null = null; | ||
|
|
||
| isInitialized: boolean = false; | ||
|
|
||
| private constructor() { | ||
| if (PluginState.instance) { | ||
| throw new Error(PluginStateErrors.ALREADY_INITIALIZED); | ||
| } | ||
| PluginState.instance = this; | ||
| } | ||
|
|
||
| static getInstance() { | ||
| if (!PluginState.instance) { | ||
| PluginState.instance = new PluginState(); | ||
| } | ||
|
|
||
| return PluginState.instance; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| export * from './nodeProcessing'; | ||
| export * from './PluginState'; |
53 changes: 53 additions & 0 deletions
53
packages/react-native-babel-plugin/src/utils/nodeProcessing.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| import type * as Babel from '@babel/core'; | ||
|
|
||
| import type { AssignmentNode, BabelTypes } from '../types'; | ||
|
|
||
| export function insertAtProgramTop( | ||
| path: Babel.NodePath<Babel.types.Program>, | ||
| node: Babel.types.Statement | Babel.types.ModuleDeclaration | ||
| ) { | ||
| path.unshiftContainer('body', node); | ||
| } | ||
|
|
||
| export function getFileInfo(data: Babel.PluginPass) { | ||
| const result: { path: string | null; name: string | null } = { | ||
| path: null, | ||
| name: null | ||
| }; | ||
|
|
||
| if (!data.filename) { | ||
| return result; | ||
| } | ||
|
|
||
| const pathArray = data.filename.split('/'); | ||
| result.name = pathArray.slice(-1)[0]; | ||
| result.path = pathArray.slice(0, -1).join('/'); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export function getAssignmentNode( | ||
| t: BabelTypes, | ||
| objectKey: string, | ||
| propertyKey: string, | ||
| value: AssignmentNode | ||
| ) { | ||
| const node = t.expressionStatement( | ||
| t.assignmentExpression( | ||
| '=', | ||
| t.memberExpression( | ||
| t.identifier(objectKey), | ||
| t.identifier(propertyKey) | ||
| ), | ||
| value | ||
| ) | ||
| ); | ||
|
|
||
| return node; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "../../tsconfig", | ||
| "include": ["src", "types"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.