Skip to content

Commit

Permalink
feat: Make @sentry/browser more treeshakeable (#2747)
Browse files Browse the repository at this point in the history
* feat: Make @sentry/browser more treeshakeable

For Electron

* ref: Change the imports

* fix: Add pure and hide functions

* ref: Remove PURE
  • Loading branch information
HazAT committed Jul 17, 2020
1 parent 9041c1d commit 0f07871
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 106 deletions.
28 changes: 4 additions & 24 deletions packages/browser/src/backend.ts
@@ -1,8 +1,8 @@
import { BaseBackend } from '@sentry/core';
import { Event, EventHint, Options, Severity, Transport } from '@sentry/types';
import { addExceptionMechanism, supportsFetch, SyncPromise } from '@sentry/utils';
import { supportsFetch } from '@sentry/utils';

import { eventFromString, eventFromUnknownInput } from './eventbuilder';
import { eventFromException, eventFromMessage } from './eventbuilder';
import { FetchTransport, XHRTransport } from './transports';

/**
Expand Down Expand Up @@ -63,32 +63,12 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
* @inheritDoc
*/
public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromUnknownInput(exception, syntheticException, {
attachStacktrace: this._options.attachStacktrace,
});
addExceptionMechanism(event, {
handled: true,
type: 'generic',
});
event.level = Severity.Error;
if (hint && hint.event_id) {
event.event_id = hint.event_id;
}
return SyncPromise.resolve(event);
return eventFromException(this._options, exception, hint);
}
/**
* @inheritDoc
*/
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromString(message, syntheticException, {
attachStacktrace: this._options.attachStacktrace,
});
event.level = level;
if (hint && hint.event_id) {
event.event_id = hint.event_id;
}
return SyncPromise.resolve(event);
return eventFromMessage(this._options, message, level, hint);
}
}
42 changes: 4 additions & 38 deletions packages/browser/src/client.ts
@@ -1,38 +1,12 @@
import { API, BaseClient, Scope } from '@sentry/core';
import { DsnLike, Event, EventHint } from '@sentry/types';
import { BaseClient, Scope } from '@sentry/core';
import { Event, EventHint } from '@sentry/types';
import { getGlobalObject, logger } from '@sentry/utils';

import { BrowserBackend, BrowserOptions } from './backend';
import { injectReportDialog, ReportDialogOptions } from './helpers';
import { Breadcrumbs } from './integrations';
import { SDK_NAME, SDK_VERSION } from './version';

/**
* All properties the report dialog supports
*/
export interface ReportDialogOptions {
[key: string]: any;
eventId?: string;
dsn?: DsnLike;
user?: {
email?: string;
name?: string;
};
lang?: string;
title?: string;
subtitle?: string;
subtitle2?: string;
labelName?: string;
labelEmail?: string;
labelComments?: string;
labelClose?: string;
labelSubmit?: string;
errorGeneric?: string;
errorFormEntry?: string;
successMessage?: string;
/** Callback after reportDialog showed up */
onLoad?(): void;
}

/**
* The Sentry Browser SDK Client.
*
Expand Down Expand Up @@ -110,14 +84,6 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
return;
}

const script = document.createElement('script');
script.async = true;
script.src = new API(dsn).getReportDialogEndpoint(options);

if (options.onLoad) {
script.onload = options.onLoad;
}

(document.head || document.body).appendChild(script);
injectReportDialog(options);
}
}
53 changes: 49 additions & 4 deletions packages/browser/src/eventbuilder.ts
@@ -1,4 +1,4 @@
import { Event } from '@sentry/types';
import { Event, EventHint, Options, Severity } from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
Expand All @@ -8,12 +8,56 @@ import {
isErrorEvent,
isEvent,
isPlainObject,
SyncPromise,
} from '@sentry/utils';

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

/** JSDoc */
/**
* Builds and Event from a Exception
* @hidden
*/
export function eventFromException(options: Options, exception: any, hint?: EventHint): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromUnknownInput(exception, syntheticException, {
attachStacktrace: options.attachStacktrace,
});
addExceptionMechanism(event, {
handled: true,
type: 'generic',
});
event.level = Severity.Error;
if (hint && hint.event_id) {
event.event_id = hint.event_id;
}
return SyncPromise.resolve(event);
}

/**
* Builds and Event from a Message
* @hidden
*/
export function eventFromMessage(
options: Options,
message: string,
level: Severity = Severity.Info,
hint?: EventHint,
): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromString(message, syntheticException, {
attachStacktrace: options.attachStacktrace,
});
event.level = level;
if (hint && hint.event_id) {
event.event_id = hint.event_id;
}
return SyncPromise.resolve(event);
}

/**
* @hidden
*/
export function eventFromUnknownInput(
exception: unknown,
syntheticException?: Error,
Expand Down Expand Up @@ -79,8 +123,9 @@ export function eventFromUnknownInput(
return event;
}

// this._options.attachStacktrace
/** JSDoc */
/**
* @hidden
*/
export function eventFromString(
input: string,
syntheticException?: Error,
Expand Down
4 changes: 3 additions & 1 deletion packages/browser/src/exports.ts
Expand Up @@ -38,6 +38,8 @@ export {
} from '@sentry/core';

export { BrowserOptions } from './backend';
export { BrowserClient, ReportDialogOptions } from './client';
export { BrowserClient } from './client';
export { injectReportDialog, ReportDialogOptions } from './helpers';
export { eventFromException, eventFromMessage } from './eventbuilder';
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close, wrap } from './sdk';
export { SDK_NAME, SDK_VERSION } from './version';
48 changes: 46 additions & 2 deletions packages/browser/src/helpers.ts
@@ -1,5 +1,5 @@
import { captureException, withScope } from '@sentry/core';
import { Event as SentryEvent, Mechanism, Scope, WrappedFunction } from '@sentry/types';
import { API, captureException, withScope } from '@sentry/core';
import { DsnLike, Event as SentryEvent, Mechanism, Scope, WrappedFunction } from '@sentry/types';
import { addExceptionMechanism, addExceptionTypeValue } from '@sentry/utils';

let ignoreOnError: number = 0;
Expand Down Expand Up @@ -158,3 +158,47 @@ export function wrap(

return sentryWrapped;
}

/**
* All properties the report dialog supports
*/
export interface ReportDialogOptions {
[key: string]: any;
eventId?: string;
dsn?: DsnLike;
user?: {
email?: string;
name?: string;
};
lang?: string;
title?: string;
subtitle?: string;
subtitle2?: string;
labelName?: string;
labelEmail?: string;
labelComments?: string;
labelClose?: string;
labelSubmit?: string;
errorGeneric?: string;
errorFormEntry?: string;
successMessage?: string;
/** Callback after reportDialog showed up */
onLoad?(): void;
}

/**
* Injects the Report Dialog script
* @hidden
*/
export function injectReportDialog(options: ReportDialogOptions = {}): void {
const script = document.createElement('script');
script.async = true;
// tslint:disable-next-line: no-non-null-assertion
script.src = new API(options.dsn!).getReportDialogEndpoint(options);

if (options.onLoad) {
script.onload = options.onLoad;
}

(document.head || document.body).appendChild(script);
}
4 changes: 2 additions & 2 deletions packages/browser/src/sdk.ts
Expand Up @@ -2,8 +2,8 @@ import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@s
import { getGlobalObject, SyncPromise } from '@sentry/utils';

import { BrowserOptions } from './backend';
import { BrowserClient, ReportDialogOptions } from './client';
import { wrap as internalWrap } from './helpers';
import { BrowserClient } from './client';
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
import { Breadcrumbs, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';

export const defaultIntegrations = [
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/index.ts
Expand Up @@ -37,7 +37,7 @@ export {
withScope,
} from '@sentry/core';

export { NodeOptions } from './backend';
export { NodeBackend, NodeOptions } from './backend';
export { NodeClient } from './client';
export { defaultIntegrations, init, lastEventId, flush, close } from './sdk';
export { SDK_NAME, SDK_VERSION } from './version';
Expand Down
35 changes: 1 addition & 34 deletions yarn.lock
Expand Up @@ -1833,32 +1833,11 @@ after@0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f"

agent-base@4, agent-base@^4.3.0:
version "4.3.0"
resolved "https://registry.npmjs.org/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==
dependencies:
es6-promisify "^5.0.0"

agent-base@5:
agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c"
integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==

agent-base@6:
version "6.0.0"
resolved "https://registry.npmjs.org/agent-base/-/agent-base-6.0.0.tgz#5d0101f19bbfaed39980b22ae866de153b93f09a"
integrity sha512-j1Q7cSCqN+AwrmDd+pzgqc0/NpC655x2bUf5ZjRIO77DcNBFmh+OgRNzF6OKdCC9RSCb19fGd99+bhXFdkRNqw==
dependencies:
debug "4"

agent-base@~4.2.0:
version "4.2.1"
resolved "https://registry.npmjs.org/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg==
dependencies:
es6-promisify "^5.0.0"

agentkeepalive@^3.4.1:
version "3.5.2"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67"
Expand Down Expand Up @@ -4619,18 +4598,6 @@ es-to-primitive@^1.1.1, es-to-primitive@^1.2.0:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es6-promise@^4.0.3:
version "4.2.8"
resolved "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==

es6-promisify@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=
dependencies:
es6-promise "^4.0.3"

escape-html@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
Expand Down

0 comments on commit 0f07871

Please sign in to comment.