Skip to content

Commit 0f07871

Browse files
authored
feat: Make @sentry/browser more treeshakeable (getsentry#2747)
* feat: Make @sentry/browser more treeshakeable For Electron * ref: Change the imports * fix: Add pure and hide functions * ref: Remove PURE
1 parent 9041c1d commit 0f07871

File tree

8 files changed

+110
-106
lines changed

8 files changed

+110
-106
lines changed

packages/browser/src/backend.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { BaseBackend } from '@sentry/core';
22
import { Event, EventHint, Options, Severity, Transport } from '@sentry/types';
3-
import { addExceptionMechanism, supportsFetch, SyncPromise } from '@sentry/utils';
3+
import { supportsFetch } from '@sentry/utils';
44

5-
import { eventFromString, eventFromUnknownInput } from './eventbuilder';
5+
import { eventFromException, eventFromMessage } from './eventbuilder';
66
import { FetchTransport, XHRTransport } from './transports';
77

88
/**
@@ -63,32 +63,12 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
6363
* @inheritDoc
6464
*/
6565
public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
66-
const syntheticException = (hint && hint.syntheticException) || undefined;
67-
const event = eventFromUnknownInput(exception, syntheticException, {
68-
attachStacktrace: this._options.attachStacktrace,
69-
});
70-
addExceptionMechanism(event, {
71-
handled: true,
72-
type: 'generic',
73-
});
74-
event.level = Severity.Error;
75-
if (hint && hint.event_id) {
76-
event.event_id = hint.event_id;
77-
}
78-
return SyncPromise.resolve(event);
66+
return eventFromException(this._options, exception, hint);
7967
}
8068
/**
8169
* @inheritDoc
8270
*/
8371
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
84-
const syntheticException = (hint && hint.syntheticException) || undefined;
85-
const event = eventFromString(message, syntheticException, {
86-
attachStacktrace: this._options.attachStacktrace,
87-
});
88-
event.level = level;
89-
if (hint && hint.event_id) {
90-
event.event_id = hint.event_id;
91-
}
92-
return SyncPromise.resolve(event);
72+
return eventFromMessage(this._options, message, level, hint);
9373
}
9474
}

packages/browser/src/client.ts

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
1-
import { API, BaseClient, Scope } from '@sentry/core';
2-
import { DsnLike, Event, EventHint } from '@sentry/types';
1+
import { BaseClient, Scope } from '@sentry/core';
2+
import { Event, EventHint } from '@sentry/types';
33
import { getGlobalObject, logger } from '@sentry/utils';
44

55
import { BrowserBackend, BrowserOptions } from './backend';
6+
import { injectReportDialog, ReportDialogOptions } from './helpers';
67
import { Breadcrumbs } from './integrations';
78
import { SDK_NAME, SDK_VERSION } from './version';
89

9-
/**
10-
* All properties the report dialog supports
11-
*/
12-
export interface ReportDialogOptions {
13-
[key: string]: any;
14-
eventId?: string;
15-
dsn?: DsnLike;
16-
user?: {
17-
email?: string;
18-
name?: string;
19-
};
20-
lang?: string;
21-
title?: string;
22-
subtitle?: string;
23-
subtitle2?: string;
24-
labelName?: string;
25-
labelEmail?: string;
26-
labelComments?: string;
27-
labelClose?: string;
28-
labelSubmit?: string;
29-
errorGeneric?: string;
30-
errorFormEntry?: string;
31-
successMessage?: string;
32-
/** Callback after reportDialog showed up */
33-
onLoad?(): void;
34-
}
35-
3610
/**
3711
* The Sentry Browser SDK Client.
3812
*
@@ -110,14 +84,6 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
11084
return;
11185
}
11286

113-
const script = document.createElement('script');
114-
script.async = true;
115-
script.src = new API(dsn).getReportDialogEndpoint(options);
116-
117-
if (options.onLoad) {
118-
script.onload = options.onLoad;
119-
}
120-
121-
(document.head || document.body).appendChild(script);
87+
injectReportDialog(options);
12288
}
12389
}

packages/browser/src/eventbuilder.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event } from '@sentry/types';
1+
import { Event, EventHint, Options, Severity } from '@sentry/types';
22
import {
33
addExceptionMechanism,
44
addExceptionTypeValue,
@@ -8,12 +8,56 @@ import {
88
isErrorEvent,
99
isEvent,
1010
isPlainObject,
11+
SyncPromise,
1112
} from '@sentry/utils';
1213

1314
import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from './parsers';
1415
import { computeStackTrace } from './tracekit';
1516

16-
/** JSDoc */
17+
/**
18+
* Builds and Event from a Exception
19+
* @hidden
20+
*/
21+
export function eventFromException(options: Options, exception: any, hint?: EventHint): PromiseLike<Event> {
22+
const syntheticException = (hint && hint.syntheticException) || undefined;
23+
const event = eventFromUnknownInput(exception, syntheticException, {
24+
attachStacktrace: options.attachStacktrace,
25+
});
26+
addExceptionMechanism(event, {
27+
handled: true,
28+
type: 'generic',
29+
});
30+
event.level = Severity.Error;
31+
if (hint && hint.event_id) {
32+
event.event_id = hint.event_id;
33+
}
34+
return SyncPromise.resolve(event);
35+
}
36+
37+
/**
38+
* Builds and Event from a Message
39+
* @hidden
40+
*/
41+
export function eventFromMessage(
42+
options: Options,
43+
message: string,
44+
level: Severity = Severity.Info,
45+
hint?: EventHint,
46+
): PromiseLike<Event> {
47+
const syntheticException = (hint && hint.syntheticException) || undefined;
48+
const event = eventFromString(message, syntheticException, {
49+
attachStacktrace: options.attachStacktrace,
50+
});
51+
event.level = level;
52+
if (hint && hint.event_id) {
53+
event.event_id = hint.event_id;
54+
}
55+
return SyncPromise.resolve(event);
56+
}
57+
58+
/**
59+
* @hidden
60+
*/
1761
export function eventFromUnknownInput(
1862
exception: unknown,
1963
syntheticException?: Error,
@@ -79,8 +123,9 @@ export function eventFromUnknownInput(
79123
return event;
80124
}
81125

82-
// this._options.attachStacktrace
83-
/** JSDoc */
126+
/**
127+
* @hidden
128+
*/
84129
export function eventFromString(
85130
input: string,
86131
syntheticException?: Error,

packages/browser/src/exports.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export {
3838
} from '@sentry/core';
3939

4040
export { BrowserOptions } from './backend';
41-
export { BrowserClient, ReportDialogOptions } from './client';
41+
export { BrowserClient } from './client';
42+
export { injectReportDialog, ReportDialogOptions } from './helpers';
43+
export { eventFromException, eventFromMessage } from './eventbuilder';
4244
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close, wrap } from './sdk';
4345
export { SDK_NAME, SDK_VERSION } from './version';

packages/browser/src/helpers.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { captureException, withScope } from '@sentry/core';
2-
import { Event as SentryEvent, Mechanism, Scope, WrappedFunction } from '@sentry/types';
1+
import { API, captureException, withScope } from '@sentry/core';
2+
import { DsnLike, Event as SentryEvent, Mechanism, Scope, WrappedFunction } from '@sentry/types';
33
import { addExceptionMechanism, addExceptionTypeValue } from '@sentry/utils';
44

55
let ignoreOnError: number = 0;
@@ -158,3 +158,47 @@ export function wrap(
158158

159159
return sentryWrapped;
160160
}
161+
162+
/**
163+
* All properties the report dialog supports
164+
*/
165+
export interface ReportDialogOptions {
166+
[key: string]: any;
167+
eventId?: string;
168+
dsn?: DsnLike;
169+
user?: {
170+
email?: string;
171+
name?: string;
172+
};
173+
lang?: string;
174+
title?: string;
175+
subtitle?: string;
176+
subtitle2?: string;
177+
labelName?: string;
178+
labelEmail?: string;
179+
labelComments?: string;
180+
labelClose?: string;
181+
labelSubmit?: string;
182+
errorGeneric?: string;
183+
errorFormEntry?: string;
184+
successMessage?: string;
185+
/** Callback after reportDialog showed up */
186+
onLoad?(): void;
187+
}
188+
189+
/**
190+
* Injects the Report Dialog script
191+
* @hidden
192+
*/
193+
export function injectReportDialog(options: ReportDialogOptions = {}): void {
194+
const script = document.createElement('script');
195+
script.async = true;
196+
// tslint:disable-next-line: no-non-null-assertion
197+
script.src = new API(options.dsn!).getReportDialogEndpoint(options);
198+
199+
if (options.onLoad) {
200+
script.onload = options.onLoad;
201+
}
202+
203+
(document.head || document.body).appendChild(script);
204+
}

packages/browser/src/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@s
22
import { getGlobalObject, SyncPromise } from '@sentry/utils';
33

44
import { BrowserOptions } from './backend';
5-
import { BrowserClient, ReportDialogOptions } from './client';
6-
import { wrap as internalWrap } from './helpers';
5+
import { BrowserClient } from './client';
6+
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
77
import { Breadcrumbs, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
88

99
export const defaultIntegrations = [

packages/node/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export {
3737
withScope,
3838
} from '@sentry/core';
3939

40-
export { NodeOptions } from './backend';
40+
export { NodeBackend, NodeOptions } from './backend';
4141
export { NodeClient } from './client';
4242
export { defaultIntegrations, init, lastEventId, flush, close } from './sdk';
4343
export { SDK_NAME, SDK_VERSION } from './version';

yarn.lock

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,32 +1833,11 @@ after@0.8.2:
18331833
version "0.8.2"
18341834
resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f"
18351835

1836-
agent-base@4, agent-base@^4.3.0:
1837-
version "4.3.0"
1838-
resolved "https://registry.npmjs.org/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
1839-
integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==
1840-
dependencies:
1841-
es6-promisify "^5.0.0"
1842-
1843-
agent-base@5:
1836+
agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.0:
18441837
version "5.1.1"
18451838
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c"
18461839
integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==
18471840

1848-
agent-base@6:
1849-
version "6.0.0"
1850-
resolved "https://registry.npmjs.org/agent-base/-/agent-base-6.0.0.tgz#5d0101f19bbfaed39980b22ae866de153b93f09a"
1851-
integrity sha512-j1Q7cSCqN+AwrmDd+pzgqc0/NpC655x2bUf5ZjRIO77DcNBFmh+OgRNzF6OKdCC9RSCb19fGd99+bhXFdkRNqw==
1852-
dependencies:
1853-
debug "4"
1854-
1855-
agent-base@~4.2.0:
1856-
version "4.2.1"
1857-
resolved "https://registry.npmjs.org/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
1858-
integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg==
1859-
dependencies:
1860-
es6-promisify "^5.0.0"
1861-
18621841
agentkeepalive@^3.4.1:
18631842
version "3.5.2"
18641843
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67"
@@ -4619,18 +4598,6 @@ es-to-primitive@^1.1.1, es-to-primitive@^1.2.0:
46194598
is-date-object "^1.0.1"
46204599
is-symbol "^1.0.2"
46214600

4622-
es6-promise@^4.0.3:
4623-
version "4.2.8"
4624-
resolved "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
4625-
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==
4626-
4627-
es6-promisify@^5.0.0:
4628-
version "5.0.0"
4629-
resolved "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
4630-
integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=
4631-
dependencies:
4632-
es6-promise "^4.0.3"
4633-
46344601
escape-html@~1.0.3:
46354602
version "1.0.3"
46364603
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"

0 commit comments

Comments
 (0)