Skip to content

Commit 6d9b7aa

Browse files
AbhiPrasadonurtemizkan
authored andcommitted
ref(react): Stop using parseSemvar (#4270)
We don't need the bloated method from utils, just make our own. This helps save on bundle size when user's use the `ErrorBoundary` component (which is very common).
1 parent 8d7afac commit 6d9b7aa

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

packages/react/src/errorboundary.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { captureException, ReportDialogOptions, Scope, showReportDialog, withScope } from '@sentry/browser';
2-
import { logger, parseSemver } from '@sentry/utils';
2+
import { logger } from '@sentry/utils';
33
import hoistNonReactStatics from 'hoist-non-react-statics';
44
import * as React from 'react';
55

6-
const reactVersion = parseSemver(React.version);
6+
export function isAtLeastReact17(version: string): boolean {
7+
const major = version.match(/^([^.]+)/);
8+
return major !== null && parseInt(major[0]) >= 17;
9+
}
710

811
export const UNKNOWN_COMPONENT = 'unknown';
912

@@ -71,7 +74,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
7174
// If on React version >= 17, create stack trace from componentStack param and links
7275
// to to the original error using `error.cause` otherwise relies on error param for stacktrace.
7376
// Linking errors requires the `LinkedErrors` integration be enabled.
74-
if (reactVersion.major && reactVersion.major >= 17) {
77+
if (isAtLeastReact17(React.version)) {
7578
const errorBoundaryError = new Error(error.message);
7679
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
7780
errorBoundaryError.stack = componentStack;

packages/react/test/errorboundary.test.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { fireEvent, render, screen } from '@testing-library/react';
33
import * as React from 'react';
44
import { useState } from 'react';
55

6-
import { ErrorBoundary, ErrorBoundaryProps, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary';
6+
import {
7+
ErrorBoundary,
8+
ErrorBoundaryProps,
9+
isAtLeastReact17,
10+
UNKNOWN_COMPONENT,
11+
withErrorBoundary,
12+
} from '../src/errorboundary';
713

814
const mockCaptureException = jest.fn();
915
const mockShowReportDialog = jest.fn();
@@ -326,3 +332,17 @@ describe('ErrorBoundary', () => {
326332
});
327333
});
328334
});
335+
336+
describe('isAtLeastReact17', () => {
337+
test.each([
338+
['React 15 with no patch', '15.0', false],
339+
['React 15 with no patch and no minor', '15.5', false],
340+
['React 16', '16.0.4', false],
341+
['React 17', '17.0.0', true],
342+
['React 17 with no patch', '17.4', true],
343+
['React 17 with no patch and no minor', '17', true],
344+
['React 18', '18.0.0', true],
345+
])('%s', (_: string, input: string, output: ReturnType<typeof isAtLeastReact17>) => {
346+
expect(isAtLeastReact17(input)).toBe(output);
347+
});
348+
});

0 commit comments

Comments
 (0)