Skip to content
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
67 changes: 67 additions & 0 deletions packages/react-native-babel-plugin/package.json
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 packages/react-native-babel-plugin/src/actions/global/index.ts
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);
}
9 changes: 9 additions & 0 deletions packages/react-native-babel-plugin/src/constants/global.ts
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;
7 changes: 7 additions & 0 deletions packages/react-native-babel-plugin/src/constants/index.ts
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';
31 changes: 31 additions & 0 deletions packages/react-native-babel-plugin/src/index.ts
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) {
Comment thread
cdn34dd marked this conversation as resolved.
const { path: _p, name: _name } = getFileInfo(this);
insertSetupFlag(path, api.types);
}
}
};
}
);
9 changes: 9 additions & 0 deletions packages/react-native-babel-plugin/src/types/general.ts
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 */
};
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>;
}
Comment thread
cdn34dd marked this conversation as resolved.
8 changes: 8 additions & 0 deletions packages/react-native-babel-plugin/src/types/index.ts
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';
19 changes: 19 additions & 0 deletions packages/react-native-babel-plugin/src/types/nodes.ts
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 packages/react-native-babel-plugin/src/utils/PluginState.ts
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;
}
}
8 changes: 8 additions & 0 deletions packages/react-native-babel-plugin/src/utils/index.ts
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 packages/react-native-babel-plugin/src/utils/nodeProcessing.ts
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;
}
4 changes: 4 additions & 0 deletions packages/react-native-babel-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig",
"include": ["src", "types"]
}
Loading