Skip to content

Commit 9f584bd

Browse files
committed
build(utils): Add build constant for cdn vs. npm bundles
1 parent 50817ac commit 9f584bd

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

packages/utils/src/env.ts

Lines changed: 10 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+
declare const __SENTRY_CDN_BUNDLE__: boolean | undefined;
19+
1820
/**
1921
* Figures out if we're building a browser bundle.
2022
*
@@ -23,3 +25,10 @@ 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+
* Figures out if we're a CDN or npm bundle.
31+
*/
32+
export function isCDNBundle(): 'cdn' | 'npm' {
33+
return typeof __SENTRY_CDN_BUNDLE__ !== 'undefined' && !!__SENTRY_CDN_BUNDLE__ ? 'npm' : 'cdn';
34+
}

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+
makeIsCDNBundlePlugin,
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 isCDNBundlePlugin = makeIsCDNBundlePlugin(true);
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, isCDNBundlePlugin],
152154
},
153155

154156
'.min.js': {
155157
output: {
156158
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`,
157159
},
158-
plugins: [stripDebuggingPlugin, terserPlugin],
160+
plugins: [stripDebuggingPlugin, terserPlugin, isCDNBundlePlugin],
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, terserPlugin, isCDNBundlePlugin],
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+
makeIsCDNBundlePlugin,
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 isNPMBundlePlugin = makeIsCDNBundlePlugin(false);
3436

3537
const defaultBaseConfig = {
3638
input: entrypoints,
@@ -87,6 +89,7 @@ export function makeBaseNPMConfig(options = {}) {
8789
debugBuildStatementReplacePlugin,
8890
cleanupPlugin,
8991
extractPolyfillsPlugin,
92+
isNPMBundlePlugin,
9093
],
9194

9295
// don't include imported modules from outside the package in the final output

rollup/plugins/bundlePlugins.js

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

66+
export function makeIsCDNBundlePlugin(isCDNBundle) {
67+
return replace({
68+
values: {
69+
__SENTRY_CDN_BUNDLE__: isCDNBundle,
70+
},
71+
});
72+
}
73+
6674
/**
6775
* Create a plugin to set the value of the `__SENTRY_BROWSER_BUNDLE__` magic string.
6876
*

0 commit comments

Comments
 (0)