Skip to content

Commit 8c350e7

Browse files
authored
build(utils): Add build constant for cdn vs. npm bundles (#6904)
1 parent acd6d58 commit 8c350e7

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

packages/utils/src/env.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
*
88
* Attention:
99
* This file should not be used to define constants/flags that are intended to be used for tree-shaking conducted by
10-
* users. These fags should live in their respective packages, as we identified user tooling (specifically webpack)
10+
* users. These flags should live in their respective packages, as we identified user tooling (specifically webpack)
1111
* having issues tree-shaking these constants across package boundaries.
1212
* An example for this is the __SENTRY_DEBUG__ constant. It is declared in each package individually because we want
1313
* users to be able to shake away expressions that it guards.
1414
*/
1515

1616
declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;
1717

18+
type SdkSource = 'npm' | 'cdn' | 'loader';
19+
1820
/**
1921
* Figures out if we're building a browser bundle.
2022
*
@@ -23,3 +25,11 @@ declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;
2325
export function isBrowserBundle(): boolean {
2426
return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__;
2527
}
28+
29+
/**
30+
* Get source of SDK.
31+
*/
32+
export function getSDKSource(): SdkSource {
33+
// @ts-ignore __SENTRY_SDK_SOURCE__ is injected by rollup during build process
34+
return __SENTRY_SDK_SOURCE__;
35+
}

rollup/bundleHelpers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
makeTerserPlugin,
1818
makeTSPlugin,
1919
makeExcludeReplayPlugin,
20+
makeSetSDKSourcePlugin,
2021
} from './plugins/index.js';
2122
import { mergePlugins } from './utils';
2223

@@ -141,28 +142,29 @@ export function makeBundleConfigVariants(baseConfig, options = {}) {
141142
const includeDebuggingPlugin = makeIsDebugBuildPlugin(true);
142143
const stripDebuggingPlugin = makeIsDebugBuildPlugin(false);
143144
const terserPlugin = makeTerserPlugin();
145+
const setSdkSourcePlugin = makeSetSDKSourcePlugin('cdn');
144146

145147
// The additional options to use for each variant we're going to create.
146148
const variantSpecificConfigMap = {
147149
'.js': {
148150
output: {
149151
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.js`,
150152
},
151-
plugins: [includeDebuggingPlugin],
153+
plugins: [includeDebuggingPlugin, setSdkSourcePlugin],
152154
},
153155

154156
'.min.js': {
155157
output: {
156158
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`,
157159
},
158-
plugins: [stripDebuggingPlugin, terserPlugin],
160+
plugins: [stripDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
159161
},
160162

161163
'.debug.min.js': {
162164
output: {
163165
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.debug.min.js`,
164166
},
165-
plugins: [includeDebuggingPlugin, terserPlugin],
167+
plugins: [includeDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
166168
},
167169
};
168170

rollup/npmHelpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
makeCleanupPlugin,
1414
makeSucrasePlugin,
1515
makeDebugBuildStatementReplacePlugin,
16+
makeSetSDKSourcePlugin,
1617
} from './plugins/index.js';
1718
import { mergePlugins } from './utils';
1819

@@ -31,6 +32,7 @@ export function makeBaseNPMConfig(options = {}) {
3132
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
3233
const cleanupPlugin = makeCleanupPlugin();
3334
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();
35+
const setSdkSourcePlugin = makeSetSDKSourcePlugin('npm');
3436

3537
const defaultBaseConfig = {
3638
input: entrypoints,
@@ -83,6 +85,7 @@ export function makeBaseNPMConfig(options = {}) {
8385

8486
plugins: [
8587
nodeResolvePlugin,
88+
setSdkSourcePlugin,
8689
sucrasePlugin,
8790
debugBuildStatementReplacePlugin,
8891
cleanupPlugin,

rollup/plugins/bundlePlugins.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ export function makeIsDebugBuildPlugin(includeDebugging) {
6363
});
6464
}
6565

66+
export function makeSetSDKSourcePlugin(sdkSource) {
67+
return replace({
68+
preventAssignment: false,
69+
values: {
70+
__SENTRY_SDK_SOURCE__: JSON.stringify(sdkSource),
71+
},
72+
});
73+
}
74+
6675
/**
6776
* Create a plugin to set the value of the `__SENTRY_BROWSER_BUNDLE__` magic string.
6877
*

0 commit comments

Comments
 (0)