Skip to content

Commit ccc1043

Browse files
committed
feat(tracing): Expose BrowserTracing in non-tracing bundles
In order to avoid breaking the bundler when a user opts out of tracing there.
1 parent 3269a06 commit ccc1043

File tree

11 files changed

+105
-8
lines changed

11 files changed

+105
-8
lines changed

packages/browser-integration-tests/suites/replay/replayShim/test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ sentryTest(
3030
await forceFlushReplay();
3131

3232
expect(requestCount).toBe(0);
33-
expect(consoleMessages).toEqual([
34-
'You are using new Replay() even though this bundle does not include the replay integration.',
35-
]);
33+
expect(consoleMessages).toEqual(['You are using new Replay() even though this bundle does not include replay.']);
3634
},
3735
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
sampleRate: 1,
8+
integrations: [new Sentry.Integrations.BrowserTracing()],
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button onclick="console.log('Test log')">Click me</button>
8+
</body>
9+
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../utils/fixtures';
4+
5+
sentryTest('exports a shim BrowserTracing integration for non-tracing bundles', async ({ getLocalTestPath, page }) => {
6+
const bundle = process.env.PW_BUNDLE;
7+
8+
if (!bundle || !bundle.startsWith('bundle_') || bundle.includes('tracing')) {
9+
sentryTest.skip();
10+
}
11+
12+
const consoleMessages: string[] = [];
13+
page.on('console', msg => consoleMessages.push(msg.text()));
14+
15+
let requestCount = 0;
16+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
17+
requestCount++;
18+
return route.fulfill({
19+
status: 200,
20+
contentType: 'application/json',
21+
body: JSON.stringify({ id: 'test-id' }),
22+
});
23+
});
24+
25+
const url = await getLocalTestPath({ testDir: __dirname });
26+
27+
await page.goto(url);
28+
29+
expect(requestCount).toBe(0);
30+
expect(consoleMessages).toEqual([
31+
'You are using new BrowserTracing() even though this bundle does not include tracing.',
32+
]);
33+
});

packages/browser/rollup.bundle.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const builds = [];
99
jsVersion,
1010
licenseTitle: '@sentry/browser',
1111
includeReplay: 'shim',
12+
includeBrowserTracingShim: true,
1213
outputFileBase: () => `bundles/bundle${jsVersion === 'es5' ? '.es5' : ''}`,
1314
});
1415

@@ -23,6 +24,7 @@ const replayBaseBundleConfig = makeBaseBundleConfig({
2324
licenseTitle: '@sentry/browser & @sentry/replay',
2425
outputFileBase: () => 'bundles/bundle.replay',
2526
includeReplay: true,
27+
includeBrowserTracingShim: true,
2628
});
2729

2830
builds.push(...makeBundleConfigVariants(replayBaseBundleConfig));

packages/browser/src/integrations/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ export { Breadcrumbs } from './breadcrumbs';
44
export { LinkedErrors } from './linkederrors';
55
export { HttpContext } from './httpcontext';
66
export { Dedupe } from './dedupe';
7+
8+
// __ROLLUP_EXCLUDE_BROWSER_TRACING_SHIM_FROM_BUNDLES_BEGIN__
9+
export { BrowserTracing } from '@sentry-internal/integration-shims';
10+
// __ROLLUP_EXCLUDE_BROWSER_TRACING_SHIM_FROM_BUNDLES_END__
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Integration } from '@sentry/types';
2+
3+
/**
4+
* This is a shim for the BrowserTracing integration.
5+
* It is needed in order for the CDN bundles to continue working when users add/remove tracing
6+
* from it, without changing their config. This is necessary for the loader mechanism.
7+
*/
8+
class BrowserTracingShim implements Integration {
9+
/**
10+
* @inheritDoc
11+
*/
12+
public static id: string = 'BrowserTracing';
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
public name: string = BrowserTracingShim.id;
18+
19+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20+
public constructor(_options: any) {
21+
// eslint-disable-next-line no-console
22+
console.error('You are using new BrowserTracing() even though this bundle does not include tracing.');
23+
}
24+
25+
/** jsdoc */
26+
public setupOnce(): void {
27+
// noop
28+
}
29+
}
30+
31+
export { BrowserTracingShim as BrowserTracing };

packages/integration-shims/src/Replay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ReplayShim implements Integration {
1919
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2020
public constructor(_options: any) {
2121
// eslint-disable-next-line no-console
22-
console.error('You are using new Replay() even though this bundle does not include the replay integration.');
22+
console.error('You are using new Replay() even though this bundle does not include replay.');
2323
}
2424

2525
/** jsdoc */
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './Replay';
1+
export { Replay } from './Replay';
2+
export { BrowserTracing } from './BrowserTracing';

rollup/bundleHelpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function makeBaseBundleConfig(options) {
3535
includeReplay,
3636
includeOffline,
3737
includeBrowserProfiling,
38+
includeBrowserTracingShim,
3839
} = options;
3940

4041
const nodeResolvePlugin = makeNodeResolvePlugin();
@@ -47,6 +48,7 @@ export function makeBaseBundleConfig(options) {
4748
const excludeReplayShimPlugin = makeExcludeBlockPlugin('REPLAY_SHIM');
4849
const excludeOfflineTransport = makeExcludeBlockPlugin('OFFLINE');
4950
const excludeBrowserProfiling = makeExcludeBlockPlugin('BROWSER_PROFILING');
51+
const excludeBrowserTracingShim = makeExcludeBlockPlugin('BROWSER_TRACING_SHIM');
5052
const replayShimPlugin = makeReplayShimPlugin();
5153

5254
// The `commonjs` plugin is the `esModuleInterop` of the bundling world. When used with `transformMixedEsModules`, it
@@ -82,6 +84,10 @@ export function makeBaseBundleConfig(options) {
8284
standAloneBundleConfig.plugins.push(excludeBrowserProfiling);
8385
}
8486

87+
if (!includeBrowserTracingShim) {
88+
standAloneBundleConfig.plugins.push(excludeBrowserTracingShim);
89+
}
90+
8591
// used by `@sentry/integrations` and `@sentry/wasm` (bundles which need to be combined with a stand-alone SDK bundle)
8692
const addOnBundleConfig = {
8793
// These output settings are designed to mimic an IIFE. We don't use Rollup's `iife` format because we don't want to

0 commit comments

Comments
 (0)