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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- New `ready` prop. When a screen has multiple async data sources, mount one `<TimeToFullDisplay ready={...} />` per source โ€” TTID/TTFD is recorded only when every instance reports `ready === true`.
- The existing `record` prop is unchanged BUT it is now deprecated in favor of `ready`.
- Extract text content from children of touched components as a label fallback for touch breadcrumbs ([#6106](https://github.com/getsentry/sentry-react-native/pull/6106))
- Auto-inject `sentry-label` from static text content at build time when `annotateReactComponents` is enabled ([#6141](https://github.com/getsentry/sentry-react-native/pull/6141))
- Respect Replay Mask boundaries when reading `sentry-label` for touch breadcrumbs ([#6142](https://github.com/getsentry/sentry-react-native/pull/6142))

### Fixes
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/js/tools/metroconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export interface SentryMetroConfigOptions {
| boolean
| {
ignoredComponents?: string[];
/**
* Automatically inject `sentry-label` props from static text content.
* When enabled, the Babel plugin extracts text from children of touchable
* components and injects it as a `sentry-label` prop at build time.
*
* @default true when `annotateReactComponents` is enabled
*/
autoInjectSentryLabel?: boolean;
};
/**
* Adds the Sentry replay package for web.
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/js/tools/sentryBabelTransformerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import * as process from 'process';

import type { BabelTransformer, BabelTransformerArgs } from './vendor/metro/metroBabelTransformer';

export type SentryBabelTransformerOptions = { annotateReactComponents?: { ignoredComponents?: string[] } };
export type SentryBabelTransformerOptions = {
annotateReactComponents?: {
ignoredComponents?: string[];
autoInjectSentryLabel?: boolean;
};
};

export const SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH = 'SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH';
export const SENTRY_BABEL_TRANSFORMER_OPTIONS = 'SENTRY_BABEL_TRANSFORMER_OPTIONS';
Expand Down Expand Up @@ -111,10 +116,10 @@ function addSentryComponentAnnotatePlugin(
}

if (!args.filename.includes('node_modules')) {
if (options) {
args.plugins.push([componentAnnotatePlugin, options]);
} else {
args.plugins.push(componentAnnotatePlugin);
}
const pluginOptions = {
autoInjectSentryLabel: true,
...options,
};
args.plugins.push([componentAnnotatePlugin, pluginOptions]);
}
}
47 changes: 41 additions & 6 deletions packages/core/test/tools/sentryBabelTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,25 @@ describe('SentryBabelTransformer', () => {
options: {
projectRoot: 'project/root',
},
plugins: [expect.any(Function), expect.any(Function)],
plugins: [expect.any(Function), [expect.any(Function), expect.objectContaining({ autoInjectSentryLabel: true })]],
});
expect(MockDefaultBabelTransformer.transform.mock.calls[0][0]['plugins'][1].name).toEqual(
expect(MockDefaultBabelTransformer.transform.mock.calls[0][0]['plugins'][1][0].name).toEqual(
'componentNameAnnotatePlugin',
);
});

test('transform adds plugin', () => {
test('transform adds plugin with autoInjectSentryLabel enabled by default', () => {
createSentryBabelTransformer().transform?.(createMinimalMockedTransformOptions());

expect(MockDefaultBabelTransformer.transform).toHaveBeenCalledTimes(1);
expect(MockDefaultBabelTransformer.transform).toHaveBeenCalledWith(
expect.objectContaining({
plugins: expect.arrayContaining([expect.objectContaining({ name: 'componentNameAnnotatePlugin' })]),
plugins: expect.arrayContaining([
[
expect.objectContaining({ name: 'componentNameAnnotatePlugin' }),
expect.objectContaining({ autoInjectSentryLabel: true }),
],
]),
}),
);
});
Expand All @@ -79,6 +84,7 @@ describe('SentryBabelTransformer', () => {
[
expect.objectContaining({ name: 'componentNameAnnotatePlugin' }),
expect.objectContaining({
autoInjectSentryLabel: true,
ignoredComponents: ['MyCustomComponent'],
}),
],
Expand All @@ -87,15 +93,44 @@ describe('SentryBabelTransformer', () => {
);
});

test('degrades gracefully if options can not be parsed, transform adds plugin without options', () => {
test('transform respects autoInjectSentryLabel: false override', () => {
process.env[SENTRY_BABEL_TRANSFORMER_OPTIONS] = JSON.stringify({
annotateReactComponents: {
autoInjectSentryLabel: false,
},
});

createSentryBabelTransformer().transform?.(createMinimalMockedTransformOptions());

expect(MockDefaultBabelTransformer.transform).toHaveBeenCalledTimes(1);
expect(MockDefaultBabelTransformer.transform).toHaveBeenCalledWith(
expect.objectContaining({
plugins: expect.arrayContaining([
[
expect.objectContaining({ name: 'componentNameAnnotatePlugin' }),
expect.objectContaining({
autoInjectSentryLabel: false,
}),
],
]),
}),
);
});

test('degrades gracefully if options can not be parsed, transform adds plugin with defaults', () => {
process.env[SENTRY_BABEL_TRANSFORMER_OPTIONS] = 'invalid json';

createSentryBabelTransformer().transform?.(createMinimalMockedTransformOptions());

expect(MockDefaultBabelTransformer.transform).toHaveBeenCalledTimes(1);
expect(MockDefaultBabelTransformer.transform).toHaveBeenCalledWith(
expect.objectContaining({
plugins: expect.arrayContaining([expect.objectContaining({ name: 'componentNameAnnotatePlugin' })]),
plugins: expect.arrayContaining([
[
expect.objectContaining({ name: 'componentNameAnnotatePlugin' }),
expect.objectContaining({ autoInjectSentryLabel: true }),
],
]),
}),
);
});
Expand Down
Loading