Skip to content

Commit e99704d

Browse files
committed
feat(react-router): Add component annotation plugin
1 parent 86331fc commit e99704d

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

packages/react-router/src/vite/makeCustomSentryVitePlugins.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export async function makeCustomSentryVitePlugins(options: SentryReactRouterBuil
1414
org,
1515
project,
1616
telemetry,
17+
reactComponentAnnotation,
1718
release,
1819
} = options;
1920

@@ -30,6 +31,11 @@ export async function makeCustomSentryVitePlugins(options: SentryReactRouterBuil
3031
},
3132
...unstable_sentryVitePluginOptions?._metaOptions,
3233
},
34+
reactComponentAnnotation: {
35+
enabled: reactComponentAnnotation?.enabled ?? undefined,
36+
ignoredComponents: reactComponentAnnotation?.ignoredComponents ?? undefined,
37+
...unstable_sentryVitePluginOptions?.reactComponentAnnotation,
38+
},
3339
release: {
3440
...unstable_sentryVitePluginOptions?.release,
3541
...release,
@@ -48,7 +54,9 @@ export async function makeCustomSentryVitePlugins(options: SentryReactRouterBuil
4854
return [
4955
'sentry-telemetry-plugin',
5056
'sentry-vite-release-injection-plugin',
51-
'sentry-vite-component-name-annotate-plugin',
57+
...(reactComponentAnnotation?.enabled || unstable_sentryVitePluginOptions?.reactComponentAnnotation?.enabled
58+
? ['sentry-vite-component-name-annotate-plugin']
59+
: []),
5260
].includes(plugin.name);
5361
}),
5462
];

packages/react-router/src/vite/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ export type SentryReactRouterBuildOptions = {
125125
*/
126126
debug?: boolean;
127127

128+
/**
129+
* Options related to react component name annotations.
130+
* Disabled by default, unless a value is set for this option.
131+
* When enabled, your app's DOM will automatically be annotated during build-time with their respective component names.
132+
* 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.
133+
* Please note that this feature is not currently supported by the esbuild bundler plugins, and will only annotate React components
134+
*/
135+
reactComponentAnnotation?: {
136+
/**
137+
* Whether the component name annotate plugin should be enabled or not.
138+
*/
139+
enabled?: boolean;
140+
141+
/**
142+
* A list of strings representing the names of components to ignore. The plugin will not apply `data-sentry` annotations on the DOM element for these components.
143+
*/
144+
ignoredComponents?: string[];
145+
};
146+
128147
/**
129148
* Options for the Sentry Vite plugin to customize the source maps upload process.
130149
*

packages/react-router/test/vite/makeCustomSentryVitePlugins.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ vi.mock('@sentry/vite-plugin', () => ({
88
.mockReturnValue([
99
{ name: 'sentry-telemetry-plugin' },
1010
{ name: 'sentry-vite-release-injection-plugin' },
11+
{ name: 'sentry-vite-component-name-annotate-plugin' },
1112
{ name: 'other-plugin' },
1213
]),
1314
}));
@@ -60,4 +61,24 @@ describe('makeCustomSentryVitePlugins', () => {
6061
expect(plugins?.[0]?.name).toBe('sentry-telemetry-plugin');
6162
expect(plugins?.[1]?.name).toBe('sentry-vite-release-injection-plugin');
6263
});
64+
65+
it('should include component annotation plugin when reactComponentAnnotation.enabled is true', async () => {
66+
const plugins = await makeCustomSentryVitePlugins({ reactComponentAnnotation: { enabled: true } });
67+
68+
expect(plugins).toHaveLength(3);
69+
expect(plugins?.[0]?.name).toBe('sentry-telemetry-plugin');
70+
expect(plugins?.[1]?.name).toBe('sentry-vite-release-injection-plugin');
71+
expect(plugins?.[2]?.name).toBe('sentry-vite-component-name-annotate-plugin');
72+
});
73+
74+
it('should include component annotation plugin when unstable_sentryVitePluginOptions.reactComponentAnnotation.enabled is true', async () => {
75+
const plugins = await makeCustomSentryVitePlugins({
76+
unstable_sentryVitePluginOptions: { reactComponentAnnotation: { enabled: true } },
77+
});
78+
79+
expect(plugins).toHaveLength(3);
80+
expect(plugins?.[0]?.name).toBe('sentry-telemetry-plugin');
81+
expect(plugins?.[1]?.name).toBe('sentry-vite-release-injection-plugin');
82+
expect(plugins?.[2]?.name).toBe('sentry-vite-component-name-annotate-plugin');
83+
});
6384
});

0 commit comments

Comments
 (0)