Skip to content

Commit aeedac2

Browse files
authored
ref(tracing): Make tracing integrations tree shakeable (#4204)
Cuts ~2kb for nextjs client gzip + minified bundle. Previously we expected users to import tracing integrations like ```js import { Integrations } from '@sentry/tracing'; const instance = new Integrations.BrowserTracing(); ``` This makes the integrations unable to be treeshaken though. To address this, we now have this individual export. We now expect users to consume BrowserTracing like so: ```js import { BrowserTracing } from '@sentry/tracing'; const instance = new BrowserTracing(); ``` This works because we avoid the object spread in the index, we instead just take advantage of js exporting the object correctly.
1 parent d17f049 commit aeedac2

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

packages/nextjs/src/index.client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { configureScope, init as reactInit, Integrations as BrowserIntegrations } from '@sentry/react';
2-
import { defaultRequestInstrumentationOptions, Integrations as TracingIntegrations } from '@sentry/tracing';
2+
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';
33

44
import { nextRouterInstrumentation } from './performance/client';
55
import { MetadataBuilder } from './utils/metadataBuilder';
@@ -9,7 +9,6 @@ import { addIntegration, UserIntegrations } from './utils/userIntegrations';
99
export * from '@sentry/react';
1010
export { nextRouterInstrumentation } from './performance/client';
1111

12-
const { BrowserTracing } = TracingIntegrations;
1312
export const Integrations = { ...BrowserIntegrations, BrowserTracing };
1413

1514
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */

packages/tracing/src/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
import { BrowserTracing } from './browser';
21
import { addExtensionMethods } from './hubextensions';
3-
import * as TracingIntegrations from './integrations';
4-
5-
const Integrations = { ...TracingIntegrations, BrowserTracing };
2+
import * as Integrations from './integrations';
63

74
export { Integrations };
5+
6+
// This is already exported as part of `Integrations` above (and for the moment will remain so for
7+
// backwards compatibility), but that interferes with treeshaking, so we also export it separately
8+
// here.
9+
//
10+
// Previously we expected users to import tracing integrations like
11+
//
12+
// import { Integrations } from '@sentry/tracing';
13+
// const instance = new Integrations.BrowserTracing();
14+
//
15+
// This makes the integrations unable to be treeshaken though. To address this, we now have
16+
// this individual export. We now expect users to consume BrowserTracing like so:
17+
//
18+
// import { BrowserTracing } from '@sentry/tracing';
19+
// const instance = new BrowserTracing();
20+
//
21+
// For an example of of the new usage of BrowserTracing, see @sentry/nextjs index.client.ts
22+
export { BrowserTracing } from './browser';
23+
824
export { Span } from './span';
925
export { Transaction } from './transaction';
1026
export {

packages/tracing/src/integrations/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ export { Express } from './node/express';
22
export { Postgres } from './node/postgres';
33
export { Mysql } from './node/mysql';
44
export { Mongo } from './node/mongo';
5+
6+
// TODO(v7): Remove this export
7+
// Please see `src/index.ts` for more details.
8+
export { BrowserTracing } from '../browser';
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Integrations } from '../src/index.bundle';
2-
import { testOnlyIfNodeVersionAtLeast } from './testutils';
32

43
describe('Integrations export', () => {
5-
// TODO `Object.values` doesn't work on Node < 8
6-
testOnlyIfNodeVersionAtLeast(8)('is exported correctly', () => {
7-
Object.values(Integrations).forEach(integration => {
8-
expect(integration.id).toStrictEqual(expect.any(String));
4+
it('is exported correctly', () => {
5+
Object.keys(Integrations).forEach(key => {
6+
expect(Integrations[key as keyof typeof Integrations].id).toStrictEqual(expect.any(String));
97
});
108
});
119
});

packages/tracing/test/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getCurrentHub } from '@sentry/hub';
2+
3+
import { BrowserTracing, Integrations } from '../src';
4+
5+
describe('index', () => {
6+
it('patches the global hub to add an implementation for `Hub.startTransaction` as a side effect', () => {
7+
const hub = getCurrentHub();
8+
const transaction = hub.startTransaction({ name: 'test', endTimestamp: 123 });
9+
expect(transaction).toBeDefined();
10+
});
11+
12+
describe('Integrations', () => {
13+
it('is exported correctly', () => {
14+
Object.keys(Integrations).forEach(key => {
15+
expect(Integrations[key as keyof typeof Integrations].id).toStrictEqual(expect.any(String));
16+
});
17+
});
18+
19+
it('contains BrowserTracing', () => {
20+
expect(Integrations.BrowserTracing).toEqual(BrowserTracing);
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)