Skip to content

Commit aab05e4

Browse files
committed
Revert "ref: Do not use build-time constant for SDK source at all ??"
This reverts commit 711e454.
1 parent 10a556f commit aab05e4

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

dev-packages/rollup-utils/bundleHelpers.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
makeLicensePlugin,
1616
makeNodeResolvePlugin,
1717
makeRrwebBuildPlugin,
18+
makeSetSDKSourcePlugin,
1819
makeSucrasePlugin,
1920
makeTerserPlugin,
2021
} from './plugins/index.mjs';
@@ -50,7 +51,6 @@ export function makeBaseBundleConfig(options) {
5051
intro: () => {
5152
return 'exports = window.Sentry || {};';
5253
},
53-
banner: 'window.SENTRY_SDK_SOURCE = window.SENTRY_SDK_SOURCE || "cdn";',
5454
},
5555
context: 'window',
5656
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin],
@@ -168,28 +168,29 @@ export function makeBundleConfigVariants(baseConfig, options = {}) {
168168
const includeDebuggingPlugin = makeIsDebugBuildPlugin(true);
169169
const stripDebuggingPlugin = makeIsDebugBuildPlugin(false);
170170
const terserPlugin = makeTerserPlugin();
171+
const setSdkSourcePlugin = makeSetSDKSourcePlugin('cdn');
171172

172173
// The additional options to use for each variant we're going to create.
173174
const variantSpecificConfigMap = {
174175
'.js': {
175176
output: {
176177
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.js`,
177178
},
178-
plugins: [includeDebuggingPlugin],
179+
plugins: [includeDebuggingPlugin, setSdkSourcePlugin],
179180
},
180181

181182
'.min.js': {
182183
output: {
183184
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`,
184185
},
185-
plugins: [stripDebuggingPlugin, terserPlugin],
186+
plugins: [stripDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
186187
},
187188

188189
'.debug.min.js': {
189190
output: {
190191
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.debug.min.js`,
191192
},
192-
plugins: [includeDebuggingPlugin, terserPlugin],
193+
plugins: [includeDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
193194
},
194195
};
195196

dev-packages/rollup-utils/plugins/bundlePlugins.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export function makeIsDebugBuildPlugin(includeDebugging) {
6060
});
6161
}
6262

63+
export function makeSetSDKSourcePlugin(sdkSource) {
64+
return replace({
65+
preventAssignment: false,
66+
values: {
67+
__SENTRY_SDK_SOURCE__: JSON.stringify(sdkSource),
68+
},
69+
});
70+
}
71+
6372
/**
6473
* Create a plugin to set the value of the `__SENTRY_BROWSER_BUNDLE__` magic string.
6574
*

packages/browser/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
SeverityLevel,
1313
UserFeedback,
1414
} from '@sentry/types';
15-
import { logger } from '@sentry/utils';
15+
import { getSDKSource, logger } from '@sentry/utils';
1616

1717
import { DEBUG_BUILD } from './debug-build';
1818
import { eventFromException, eventFromMessage } from './eventbuilder';
@@ -57,7 +57,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
5757
parentSpanIsAlwaysRootSpan: true,
5858
...options,
5959
};
60-
const sdkSource = WINDOW.SENTRY_SDK_SOURCE || 'npm';
60+
const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource();
6161
applySdkMetadata(opts, 'browser', ['browser'], sdkSource);
6262

6363
super(opts);

packages/browser/test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
inboundFiltersIntegration,
1414
lastEventId,
1515
} from '@sentry/core';
16+
import * as utils from '@sentry/utils';
1617

1718
import { setCurrentClient } from '../src';
1819
import {
@@ -381,6 +382,17 @@ describe('SentryBrowser initialization', () => {
381382
delete global.SENTRY_SDK_SOURCE;
382383
});
383384

385+
it('uses SDK source from global for package name', () => {
386+
const spy = vi.spyOn(utils, 'getSDKSource').mockReturnValue('cdn');
387+
init({ dsn });
388+
389+
const sdkData = getClient()?.getOptions()._metadata?.sdk || {};
390+
391+
expect(sdkData.packages?.[0]?.name).toBe('cdn:@sentry/browser');
392+
expect(utils.getSDKSource).toBeCalledTimes(1);
393+
spy.mockRestore();
394+
});
395+
384396
it('should set SDK data when instantiating a client directly', () => {
385397
const options = getDefaultBrowserClientOptions({ dsn });
386398
const client = new BrowserClient(options);

packages/utils/src/env.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;
1717

18+
declare const __SENTRY_SDK_SOURCE__: SdkSource | undefined;
19+
1820
export type SdkSource = 'npm' | 'cdn' | 'loader';
1921

2022
/**
@@ -25,3 +27,10 @@ export type SdkSource = 'npm' | 'cdn' | 'loader';
2527
export function isBrowserBundle(): boolean {
2628
return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__;
2729
}
30+
31+
/**
32+
* Get source of SDK.
33+
*/
34+
export function getSDKSource(): SdkSource {
35+
return typeof __SENTRY_SDK_SOURCE__ !== 'undefined' ? __SENTRY_SDK_SOURCE__ : 'npm';
36+
}

0 commit comments

Comments
 (0)