Skip to content

Commit 4d123db

Browse files
0CaloriesgggritsoAbhiPrasad
authored
feat(component-annotate): Introduce new plugin to annotate frontend components at build-time (#468)
This PR in particular adds a new package to the bundler plugins; a Babel plugin that will annotate frontend components at build-time with additional data. A lot of the source code is vendored from FullStory's React annotate plugin, but has been modified and converted to TypeScript. For now, the plugin only works on React, but we can extend it to other frontend frameworks / libraries in the future. --------- Co-authored-by: George Gritsouk <[email protected]> Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 1fff206 commit 4d123db

File tree

17 files changed

+3810
-0
lines changed

17 files changed

+3810
-0
lines changed

.craft.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ requireNames:
99
- /^sentry-rollup-plugin-.*\.tgz$/
1010
- /^sentry-vite-plugin-.*\.tgz$/
1111
- /^sentry-webpack-plugin-.*\.tgz$/
12+
- /^sentry-component-annotate-plugin-.*\.tgz$/
1213
targets:
1314
- name: github
1415
includeNames: /^sentry-.*.tgz$/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/env", "@babel/typescript"]
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const jestPackageJson = require("jest/package.json");
2+
3+
/** @type {import('eslint').ESLint.Options} */
4+
module.exports = {
5+
root: true,
6+
extends: ["@sentry-internal/eslint-config/jest", "@sentry-internal/eslint-config/base"],
7+
ignorePatterns: [".eslintrc.js", "dist", "jest.config.js", "rollup.config.js"],
8+
parserOptions: {
9+
tsconfigRootDir: __dirname,
10+
project: ["./src/tsconfig.json", "./test/tsconfig.json"],
11+
},
12+
env: {
13+
node: true,
14+
},
15+
settings: {
16+
jest: {
17+
version: jestPackageJson.version,
18+
},
19+
},
20+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
.DS_Store
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# MIT License
2+
3+
Copyright (c) 2024, Sentry
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
- Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
- Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
- Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<p align="center">
2+
<a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
4+
</a>
5+
</p>
6+
7+
# Sentry Component Annotate Plugin (Beta)
8+
9+
[![npm version](https://img.shields.io/npm/v/@sentry/component-annotate-plugin.svg)](https://www.npmjs.com/package/@sentry/component-annotate-plugin)
10+
[![npm dm](https://img.shields.io/npm/dm/@sentry/component-annotate-plugin.svg)](https://www.npmjs.com/package/@sentry/component-annotate-plugin)
11+
[![npm dt](https://img.shields.io/npm/dt/@sentry/component-annotate-plugin.svg)](https://www.npmjs.com/package/@component-annotate-plugin)
12+
13+
This plugin is currently in beta. Please help us improve by [reporting any issues or giving us feedback](https://github.com/getsentry/sentry-javascript-bundler-plugins/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc).
14+
15+
A Babel plugin that automatically annotates your output DOM with their respective frontend component names.
16+
This will unlock the capability to search for Replays in Sentry by component name, as well as see component names in breadcrumbs and performance monitoring.
17+
Please note that your Sentry JavaScript SDK version must be at least `7.91.0` to take advantage of these features.
18+
Currently, this plugin only works with React, and will exclusively parse `.jsx` and `.tsx` files.
19+
20+
### Note
21+
22+
This plugin comes included in Sentry's bundler plugins, alongside many other features to improve your Sentry workflow.
23+
It can be downloaded individually, but it is recommended that you install the bundler plugins for your respective bundler, and enable this feature through the config object.
24+
25+
Check out the supported bundler plugin packages for installation instructions:
26+
27+
- [Rollup](https://www.npmjs.com/package/@sentry/rollup-plugin)
28+
- [Vite](https://www.npmjs.com/package/@sentry/vite-plugin)
29+
- [Webpack](https://www.npmjs.com/package/@sentry/webpack-plugin)
30+
- esbuild: Not currently supported
31+
32+
## Installation
33+
34+
Using npm:
35+
36+
```bash
37+
npm install @sentry/component-annotate-plugin --save-dev
38+
```
39+
40+
Using yarn:
41+
42+
```bash
43+
yarn add @sentry/component-annotate-plugin --dev
44+
```
45+
46+
Using pnpm:
47+
48+
```bash
49+
pnpm install @sentry/component-annotate-plugin --dev
50+
```
51+
52+
## Example
53+
54+
```js
55+
// babel.config.js
56+
57+
{
58+
// ... other config above ...
59+
60+
plugins: [
61+
// Put this plugin before any other plugins you have that transform JSX code
62+
['@sentry/component-annotate-plugin']
63+
],
64+
}
65+
```
66+
67+
Or alternatively, configure the plugin by directly importing it:
68+
69+
```js
70+
// babel.config.js
71+
72+
import {componentNameAnnotatePlugin} from '@sentry/component-annotate-plugin';
73+
74+
{
75+
// ... other config above ...
76+
77+
plugins: [
78+
// Put this plugin before any other plugins you have that transform JSX code
79+
[componentNameAnnotatePlugin]
80+
],
81+
}
82+
```
83+
84+
## More information
85+
86+
- [Sentry Documentation](https://docs.sentry.io/quickstart/)
87+
- [Sentry Discord](https://discord.gg/Ww9hbqr)
88+
- [Sentry Stackoverflow](http://stackoverflow.com/questions/tagged/sentry)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
testEnvironment: "node",
3+
transform: {
4+
"^.+\\.(t|j)sx?$": ["@swc/jest"],
5+
},
6+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"name": "@sentry/component-annotate-plugin",
3+
"version": "2.10.3",
4+
"description": "A Babel plugin that annotates frontend components with additional data to enrich the experience in Sentry",
5+
"repository": "git://github.com/getsentry/sentry-javascript-bundler-plugins.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript-bundler-plugins/tree/main/packages/component-annotate-plugin",
7+
"author": "Sentry",
8+
"license": "MIT",
9+
"keywords": [
10+
"Sentry",
11+
"React",
12+
"bundler",
13+
"plugin",
14+
"babel",
15+
"component",
16+
"annotate"
17+
],
18+
"publishConfig": {
19+
"access": "public"
20+
},
21+
"files": [
22+
"dist"
23+
],
24+
"exports": {
25+
".": {
26+
"import": "./dist/esm/index.mjs",
27+
"require": "./dist/cjs/index.js",
28+
"types": "./dist/types/index.d.ts"
29+
}
30+
},
31+
"main": "dist/cjs/index.js",
32+
"module": "dist/esm/index.mjs",
33+
"types": "dist/types/index.d.ts",
34+
"scripts": {
35+
"build": "rimraf ./out && run-p build:rollup build:types",
36+
"build:watch": "run-p build:rollup:watch build:types:watch",
37+
"build:rollup": "rollup --config rollup.config.js",
38+
"build:rollup:watch": "rollup --config rollup.config.js --watch --no-watch.clearScreen",
39+
"build:types": "tsc --project types.tsconfig.json",
40+
"build:types:watch": "tsc --project types.tsconfig.json --watch --preserveWatchOutput",
41+
"build:npm": "npm pack",
42+
"check:types": "run-p check:types:src check:types:test",
43+
"check:types:src": "tsc --project ./src/tsconfig.json --noEmit",
44+
"check:types:test": "tsc --project ./test/tsconfig.json --noEmit",
45+
"clean": "run-s clean:build",
46+
"clean:all": "run-p clean clean:deps",
47+
"clean:build": "rimraf ./dist *.tgz",
48+
"clean:deps": "rimraf node_modules",
49+
"test": "jest",
50+
"lint": "eslint ./src ./test",
51+
"prepack": "ts-node ./src/prepack.ts"
52+
},
53+
"dependencies": {},
54+
"devDependencies": {
55+
"@babel/core": "7.18.5",
56+
"@babel/preset-env": "7.18.2",
57+
"@babel/preset-react": "^7.23.3",
58+
"@babel/preset-typescript": "7.17.12",
59+
"@rollup/plugin-babel": "5.3.1",
60+
"@rollup/plugin-node-resolve": "13.3.0",
61+
"@sentry-internal/eslint-config": "2.10.3",
62+
"@sentry-internal/sentry-bundler-plugin-tsconfig": "2.10.3",
63+
"@swc/core": "^1.2.205",
64+
"@swc/jest": "^0.2.21",
65+
"@types/jest": "^28.1.3",
66+
"@types/node": "^18.6.3",
67+
"@types/uuid": "^9.0.1",
68+
"eslint": "^8.18.0",
69+
"jest": "^28.1.1",
70+
"rimraf": "^3.0.2",
71+
"rollup": "2.75.7",
72+
"ts-node": "^10.9.1",
73+
"typescript": "^4.7.4"
74+
},
75+
"volta": {
76+
"extends": "../../package.json"
77+
},
78+
"engines": {
79+
"node": ">= 14"
80+
}
81+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import resolve from "@rollup/plugin-node-resolve";
2+
import babel from "@rollup/plugin-babel";
3+
import packageJson from "./package.json";
4+
import modulePackage from "module";
5+
6+
const input = ["src/index.ts"];
7+
8+
const extensions = [".ts"];
9+
10+
export default {
11+
input,
12+
external: [...Object.keys(packageJson.dependencies), ...modulePackage.builtinModules],
13+
onwarn: (warning) => {
14+
throw new Error(warning.message); // Warnings are usually high-consequence for us so let's throw to catch them
15+
},
16+
plugins: [
17+
resolve({
18+
extensions,
19+
rootDir: "./src",
20+
preferBuiltins: true,
21+
}),
22+
babel({
23+
extensions,
24+
babelHelpers: "bundled",
25+
include: ["src/**/*"],
26+
}),
27+
],
28+
output: [
29+
{
30+
file: packageJson.module,
31+
format: "esm",
32+
exports: "named",
33+
sourcemap: true,
34+
},
35+
{
36+
file: packageJson.main,
37+
format: "cjs",
38+
exports: "named",
39+
sourcemap: true,
40+
},
41+
],
42+
};

0 commit comments

Comments
 (0)