Skip to content

build(utils): Add build constant for cdn vs. npm bundles #6904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/utils/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
*
* Attention:
* This file should not be used to define constants/flags that are intended to be used for tree-shaking conducted by
* users. These fags should live in their respective packages, as we identified user tooling (specifically webpack)
* users. These flags should live in their respective packages, as we identified user tooling (specifically webpack)
* having issues tree-shaking these constants across package boundaries.
* An example for this is the __SENTRY_DEBUG__ constant. It is declared in each package individually because we want
* users to be able to shake away expressions that it guards.
*/

declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;

type SdkSource = 'npm' | 'cdn' | 'loader';

/**
* Figures out if we're building a browser bundle.
*
Expand All @@ -23,3 +25,11 @@ declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;
export function isBrowserBundle(): boolean {
return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__;
}

/**
* Get source of SDK.
*/
export function getSDKSource(): SdkSource {
// @ts-ignore __SENTRY_SDK_SOURCE__ is injected by rollup during build process
return __SENTRY_SDK_SOURCE__;
}
8 changes: 5 additions & 3 deletions rollup/bundleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
makeTerserPlugin,
makeTSPlugin,
makeExcludeReplayPlugin,
makeSetSDKSourcePlugin,
} from './plugins/index.js';
import { mergePlugins } from './utils';

Expand Down Expand Up @@ -141,28 +142,29 @@ export function makeBundleConfigVariants(baseConfig, options = {}) {
const includeDebuggingPlugin = makeIsDebugBuildPlugin(true);
const stripDebuggingPlugin = makeIsDebugBuildPlugin(false);
const terserPlugin = makeTerserPlugin();
const setSdkSourcePlugin = makeSetSDKSourcePlugin('cdn');

// The additional options to use for each variant we're going to create.
const variantSpecificConfigMap = {
'.js': {
output: {
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.js`,
},
plugins: [includeDebuggingPlugin],
plugins: [includeDebuggingPlugin, setSdkSourcePlugin],
},

'.min.js': {
output: {
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`,
},
plugins: [stripDebuggingPlugin, terserPlugin],
plugins: [stripDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
},

'.debug.min.js': {
output: {
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.debug.min.js`,
},
plugins: [includeDebuggingPlugin, terserPlugin],
plugins: [includeDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
},
};

Expand Down
3 changes: 3 additions & 0 deletions rollup/npmHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
makeCleanupPlugin,
makeSucrasePlugin,
makeDebugBuildStatementReplacePlugin,
makeSetSDKSourcePlugin,
} from './plugins/index.js';
import { mergePlugins } from './utils';

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

const defaultBaseConfig = {
input: entrypoints,
Expand Down Expand Up @@ -83,6 +85,7 @@ export function makeBaseNPMConfig(options = {}) {

plugins: [
nodeResolvePlugin,
setSdkSourcePlugin,
sucrasePlugin,
debugBuildStatementReplacePlugin,
cleanupPlugin,
Expand Down
9 changes: 9 additions & 0 deletions rollup/plugins/bundlePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export function makeIsDebugBuildPlugin(includeDebugging) {
});
}

export function makeSetSDKSourcePlugin(sdkSource) {
return replace({
preventAssignment: false,
values: {
__SENTRY_SDK_SOURCE__: JSON.stringify(sdkSource),
},
});
}

/**
* Create a plugin to set the value of the `__SENTRY_BROWSER_BUNDLE__` magic string.
*
Expand Down